EF Code First 和双向导航属性中的一对一映射

发布于 2024-12-11 11:09:15 字数 1515 浏览 0 评论 0原文

我的任务是将旧应用程序移植到 MVC 3,并希望首先使用 EF 的代码来复制其现有的数据库架构。当前的代码库中充斥着硬编码的 SQL 命令,因此我提出的模型必须与当前系统的期望“兼容”。我知道我可以使用 EF 的 Database First 来执行此操作,但现有架构非常简单,我认为没有理由不使用代码优先,因为我希望在迁移旧的硬编码数据库时能够建立坚实的基础互动。

我需要 POCO 的外观:

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; }
}

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

“无法确定类型‘许可证’和‘植物’之间关联的主要端。此关联的主要端必须是使用关系流畅 API 或数据注释显式配置。”

我意识到 Plant 应该有一个对 License_Id 的可为空的 FK 引用,并且 License 不应该有 Plant_Id 的 FK。但这些是我已经拿到的牌。我想做的事在 EF 中可行吗?

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.

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:

"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."

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你没皮卡萌 2024-12-18 11:09:15
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; }
}

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

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

唯一的降级是您实际上将 1 定义为 0 或 1。 (可以在代码中处理。但仍然...)有关一对一关系的更多信息,请查看实体框架代码第一个一对一关系

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; }
}

In fluent-api (in your context class):

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

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

梦旅人picnic 2024-12-18 11:09:15

尝试添加许可证

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

// Navigational properties
[ForeignKey("Plant_Id")]
public virtual Plant Plant { get; set; }

以让它知道这是外键,其他外键也是如此。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Plant>()
            .HasOptional(e => e.License)
            .WithOptionalPrincipal(e => e.Plant);

    }

try adding in License

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

// Navigational properties
[ForeignKey("Plant_Id")]
public virtual Plant Plant { get; set; }

to let it know that this is foreign key, same for the other foreign keys.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Plant>()
            .HasOptional(e => e.License)
            .WithOptionalPrincipal(e => e.Plant);

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文