EF Code First中的一对一映射和双向导航属性 [英] One to one mapping in EF Code First and bi-directional navigation properties

查看:50
本文介绍了EF Code First中的一对一映射和双向导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是将旧的应用程序移植到MVC 3,并且想先使​​用EF的代码来复制其现有的数据库架构.当前的代码库充斥着硬编码的SQL命令,因此我想出的模型必须与当前系统的期望兼容".我知道我可以使用EF的Database First来做到这一点,但是现有的架构是如此简单,以至于我认为没有理由不首先使用代码,因为我希望在我们从旧的硬编码数据库迁移过来的基础上建立坚实的基础互动.

I have been tasked with porting an old app to MVC 3, and want to use EF's code first to replicate its existing database schema. The current code base is littered with hard coded SQL commands, and so the model I come up with has to be "compatible" with what the current system expects. I know I could use EF's Database First to do this, but the existing schema is so simple that I see no reason not to use code first, as I want a solid foundation to build on as we migrate away from our old hard-coded database interactions.

我需要什么样的POCO:

What I need the POCOs to look like:

public class Owner
{
    [Key]
    public Guid Owner_Id { get; set; }

    // Properties
    public string ContactName { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }

    // Navigational properties
    public virtual ICollection<Plant> Plants { get; set; }
}

public class Plant
{
    [Key]
    public Guid Plant_Id { get; set; }

    // Properties
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }

    // Foreign Keys
    public Guid Owner_Id { get; set; }

    // Navigational properties
    public virtual Owner Owner { get; set; }
    public virtual License License { get; set; }
}

public class License
{
    [Key] public Guid License_Id { get; set; }

    // Properties
    public int Total { get; set; }
    public int Users { get; set; }
    public string Key { get; set; }

    // Foreign Keys
    public Guid Plant_Id { get; set; }

    // Navigational properties
    public virtual Plant Plant { get; set; }
}

在尝试创建上下文时出现此错误:

It's giving me this error on attempting to create the context:

无法确定类型'License'和'Plant'之间的关联的主要末端.必须使用关系fluent API或数据注释显式配置此关联的主要末端."

"Unable to determine the principal end of an association between the types 'License' and 'Plant'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations."

我意识到Plant应该对License_Id具有可空的FK引用,并且License对Plant_Id不应该具有FK.但是这些是我已经收到的卡.我想在EF中做些什么吗?

I realize that Plant should have a nullable FK reference to a License_Id, and that License shouldn't have a FK for Plant_Id. But these are the cards I have been dealt. Is what I'm trying to do possible in EF?

推荐答案

public class Plant
{
    public Guid PlantID { get; set; }
    public virtual License License { get; set; }
} 

public class License
{
     // No need to have a PlantID foreign key since the foreign key.
     public Guid LicenseID { get; set; }
     public virtual Plant Plant { get; set; }
}

在fluent-api中(在您的上下文类中):

In fluent-api (in your context class):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Plant>().HasOptional(p => p.License).WithRequired(l => l.Plant);  

唯一的降级是您实际上定义了1到0或1.(可以用代码处理.但是仍然...)有关一对一关系的更多信息,请查看 Entity Framework Code一对一关系

The only downgrade is that you actually defined one to zero or one. (which can be handled in code. but still...) For more info about One-to-One relationship please check out Entity Framework Code First One to One Relationship

这篇关于EF Code First中的一对一映射和双向导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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