实体框架6.1和成员隐藏使用新的关键字 [英] Entity Framework 6.1 and Member Hiding using New Keyword

查看:157
本文介绍了实体框架6.1和成员隐藏使用新的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆实现所谓的接口 ICreatableEntity 实体。该定义是:

I have a bunch of entities that implement an interface called ICreatableEntity. The definition is:

public interface ICreatableEntity {
    int CreatedById { get; set; }
    Employee CreatedBy { get; set; }
    DateTime CreatedDateTime { get; set; }
}



一个具体的实体,员工,需要稍作修改,因为第一个记录不能有一个创造者,因此我需要让列空。所以,我只是想使用new关键字喜欢隐藏继承成员:

One specific entity, Employee, needs a slight modification because the very first record can't have a creator and thus I need to make the column nullable. So, I just tried to hide the inherited member using the new keyword like:

public new int? CreatedById { get; set; }

和它的所有作品,编译器会喜欢它和所有,但是当被生成模型EF,它与错误崩溃的项目已经存在的元数据收集的英寸我猜,也许EF增加我的第一个覆盖,然后继续增加基础属性为好,尽管我试图隐藏它,当它崩溃之时。

And it all works, the compiler likes it and all, but when the model is being generated by EF, it crashes with an error that the item already exists in the metadata collection. I'm guessing, EF probably adds my override in first, then proceeds to add the base property as well, even though I'm trying to hide it, and that's when it crashes.

更新:的我试图离开它是和它的流畅API配置更改可选,这也编译很好,但是当被插入,因为我在我的种子数据再次崩溃还还是把它有效地为空。

Update: I have tried to leave it as is and in the Fluent API configuration change it to Optional, which also compiles fine, but crashes yet again when my seed data is being inserted because I still leave it effectively null.

有没有这个不需要我翻转逻辑,使其可空一切,然后添加几十流利的解决方法?API的配置告诉EF它实际上需要在其他地方

Is there a workaround for this that doesn't require me to flip the logic and make it nullable for everything, and then add dozens of Fluent API configurations to tell EF it's actually required everywhere else?

更新:的只给一点点的更多信息,数据库被重新生成每通过EF的时间,因为我没有使用任何先前存在的。

Update: Just to give a little bit more information, the database is being regenerated every time by EF since I'm not using anything preexisting.

推荐答案

我发现我与相当的溶液。它击中了我,指定外键时,我可以使用 HasForeignKey()地图()用流利的API 。 HasForeignKey()要求您对实体指定一个已经存在的属性,但地图()允许您指定物业EF的名称将为您生成的外键。自从使用了两种方法的要求或关系将已配置之前,可选的( HasRequired() HasOptional() ),为空性将账户。

I found a solution that I'm quite content with. It hit me that when specifying a foreign key I can use either HasForeignKey() or Map() with the Fluent API. HasForeignKey() requires you to specify an already existing property on the entity, but Map() allows you to specify the name of the property EF will generate for you as the foreign key. Since prior to using either methods the relationship will already be configured as required or optional (HasRequired(), HasOptional()), the nullability will be account for.

所以,我只是做了这一点,从我的接口,其中自动奇迹般地解决我有全部已发行取出明确财产申报。这也帮助我开始使用基本配置类,做了很多繁重的工作对于实现我的接口的实体。我不得不一次覆盖一个具体方法为员工的配置,这是好去。

So, I just did that and removed the explicit property declarations from my interfaces which auto-magically resolved the entire issue I was having. It also helped that I started using a base configuration class that does a lot of the heavy lifting for the entities that implement my interfaces. I just had to override a specific method once for the Employee configuration and it was good to go.

下面是一个。缩短版的基本配置类,使得事情发生的

Here's a shortened version of the base configuration class that made things happen.

internal class Configuration<TEntity> :
    EntityTypeConfiguration<TEntity>
    where TEntity : class, ICreatableEntity, new() {
    protected virtual void Configure() {
        this.Configure(ConfigurationParameters.Default);
    }

    protected virtual void Configure(
        ConfigurationParameters parameters) {
        this.ConfigureCreatableRelationships(parameters.CreatedByWillCascadeOnDelete);
    }

    #region Relationship Configurations
    protected virtual void ConfigureCreatableRelationships(
        bool willCascadeOnDelete) {
        this.HasRequired(
            t =>
                t.CreatedBy).WithMany().Map(
            m =>
                m.MapKey("CreatedById")).WillCascadeOnDelete(willCascadeOnDelete);
    }
    #endregion
}



而在我重写 EmployeeConfiguration 类:

internal sealed class EmployeeConfiguration :
    Configuration<Employee> {
    protected override void ConfigureCreatableRelationships(
        bool willCascadeOnDelete) {
        this.HasOptional(
            t =>
                t.CreatedBy).WithMany().Map(
            m =>
                m.MapKey("CreatedById"));
    }
}



我希望这可以帮助别人的未来。

I hope this helps someone in the future.

这篇关于实体框架6.1和成员隐藏使用新的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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