PostSharp将k__Backing字段插入实体类,导致数据库生成失败 [英] PostSharp inserting k__Backing Field into Entity Class, causing Database generation to fail

查看:57
本文介绍了PostSharp将k__Backing字段插入实体类,导致数据库生成失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft Entity Framework和C#中的CodeFirst创建数据库。我想在WPF应用程序中使用数据库,因此实体类应实现 INotifyPropertyChanged。

I'm creating a Database using the Microsoft Entity Framework and CodeFirst in C#. I want to use the Database in a WPF-Application, so the Entity-Classes should implement "INotifyPropertyChanged".

可以使用PostSharp方面非常优雅地完成此操作,该方面每次属性更改时都会自动触发PropertyChanged事件。如果创建这样的方面并将其用于实体类,则在尝试创建数据库时会得到以下启示:

This can be done very elegantly using a PostSharp aspect, which triggers the PropertyChanged event automatically every time a property changes. If I create such an aspect and use it on my entity classes, I get the following exeption when trying to create the Database:

 \tSystem.Data.Entity.Edm.EdmNavigationProperty: Name: The specified name is not allowed:          '<Name>k__BackingField'.

显然,PostSharp创建了一个名为 k__BackingField的属性,该属性导致数据库创建失败,因为它是无效的从EntityFramework的角度来看。没有任何方法可以在每个实体类中都无需手动实现 INotifyPropertyChanged的情况下解决该错误?

Obviously PostSharp creates a property called "k__BackingField" which causes the database creation to fail, because it's an invalid name from the EntityFramework's point of view. Is there any way to circumvent this error without manually implementing "INotifyPropertyChanged" in every single Entity-Class?

PS:英语不是我的母语,我将非常感谢

P.S: English is not my native language, I would be very thankful if you informed me about possible mistakes in my postings.

在此先感谢您

推荐答案

的确,PostSharp可以创建一个称为k__BackingField的属性(真实名称有些不同;您看到的是转义名称),因为我想您将方面应用于字段,这会导致PostSharp将字段封装到具有相同名称的属性中。因此,您可以拥有一个以该字段命名的属性。

It's true that PostSharp can create a property called k__BackingField (the real name is a bit different; what you're seeing is the escapes name), because I suppose you're applying the aspect to fields, which causes PostSharp to encapsulate fields into properties of the same name. So, you can have a property named after the field.

您应检查是否确实要向该字段添加方面。也许您只是想将方面添加到属性中,但是却错误地将其添加到字段中。默认情况下,LocationInterceptionAspects适用于(多播)字段的属性。使用 MulticastAttributeUsageAttribute.AttributeTargets 将其限制为属性。

You should check whether you really want to add the aspect to the field. Perhaps you just wanted to add the aspect to properties, but are adding it to fields by mistake. By default, LocationInterceptionAspects are applied (multicast) fo fields and properties. Use MulticastAttributeUsageAttribute.AttributeTargets to restrict it to properties.

这篇关于PostSharp将k__Backing字段插入实体类,导致数据库生成失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆