简体   繁体   English

首先影响EF代码中的外键列命名(CTP5)

[英]Influencing foreign key column naming in EF code first (CTP5)

I have a POCO class that has two one-way unary relationships with another class, both classes share an ancestor. 我有一个POCO类,它与另一个类有两个单向一元关系,两个类共享一个祖先。 The names of the foreign keys in the generated schema do not reflect the property names. 生成的模式中的外键名称不反映属性名称。 (Properties MainContact and FinancialContact give PersonId and PersonId1 field names). (属性MainContact和FinancialContact给出PersonId和PersonId1字段名称)。

How can I influence schema generation to generate database column names that match the property names? 如何影响模式生成以生成与属性名称匹配的数据库列名?

The model looks like this: 该模型如下所示:

班级模特

The code looks like this: 代码如下所示:

public class CustomerContext: DbContext
{
   public DbSet<Organisation> Organisations { get; set; }
   public DbSet<Person> Persons { get; set; }

   protected override void OnModelCreating(ModelBuilder builder)
   {
      DbDatabase.SetInitializer(new DropCreateDatabaseAlways<CustomerContext>());
   }
}

public abstract class Customer
{
   public int Id { get; set; }
   public string Name { get; set; }
}

public class Person : Customer
{
   public string Email { get; set; }
}

public class Organisation : Customer
{
   public Person FinancialContact { get; set; }
   public Person MainContact { get; set; }
}

The schema looks like this: 架构如下所示: 在此输入图像描述

Answer from druttka 来自druttka的回答


druttka's answer below did the job and it's nice to know that it's a CTP5 bug that's behind this. druttka的回答完成了这项工作,很高兴知道这是一个CTP5错误。 EF also needs the cascade behaviour to be specified and I've used the fluent API to do this following the example in the link given by druttka. EF还需要指定级联行为,并且我使用了流畅的API来执行此操作,遵循druttka给出的链接中的示例。 Some more good reading from Morteza Manavi here . 来自Morteza Manavi的更好的阅读在这里

The code now is this: 代码现在是这样的:

public class CustomerContext : DbContext
{
   public DbSet<Organisation> Organisations { get; set; }
   public DbSet<Person> Persons { get; set; }

   protected override void OnModelCreating(ModelBuilder builder)
   {
      DbDatabase.SetInitializer(new DropCreateDatabaseAlways<CustomerContext>());

      builder.Entity<Organisation>()
         .HasRequired(p => p.MainContact)
         .WithMany()
         .HasForeignKey(p => p.MainContactId)
         .WillCascadeOnDelete(false);
      builder.Entity<Organisation>()
         .Property(p => p.MainContactId)
         .HasColumnName("MainContact");

      builder.Entity<Organisation>()
         .HasRequired(p => p.FinancialContact)
         .WithMany()
         .HasForeignKey(p => p.FinancialContactId)
         .WillCascadeOnDelete(false);
      builder.Entity<Organisation>()
         .Property(p => p.FinancialContactId)
         .HasColumnName("FinancialContact");
   }
}

public abstract class Customer
{
   public int Id { get; set; }
   public string Name { get; set; }
}

public class Person : Customer
{
   public string Email { get; set; }
}

public class Organisation : Customer
{
   public Person FinancialContact { get; set; }
   public int FinancialContactId { get; set; }

   public Person MainContact { get; set; }
   public int MainContactId { get; set; }
}

Which now gives the far more suitable database: 现在,它提供了更合适的数据库: 在此输入图像描述

EF Code First uses, by default, convention over configuration. 默认情况下,EF Code First使用约定优于配置。 However, you can set explicit alternatives by overriding DbContent.OnModelCreating. 但是,您可以通过覆盖DbContent.OnModelCreating来设置显式替代方案。 Many examples here , courtesy of ScottGu. 这里有许多例子,由ScottGu提供。

EDIT 编辑

So in CTP5, MapSingleType went away as described here . 所以在CTP5中,MapSingleType就像这里描述的那样消失 The following works for simple string properties, but not for your Organisation to Person relationships. 以下适用于简单字符串属性,但不适用于您的组织与人员之间的关系。 I'm curious and plan to keep looking at it, but in the meantime, maybe this will get your started or someone else can complete the answer. 我很好奇,并打算继续关注它,但与此同时,也许这会让你开始或其他人可以完成答案。

public class Person : Customer
{
    [Column(Name="EmailAddress")]
    public string Email { get; set; }
}

EDIT 2 编辑2

Ok, this gets it. 好的,这就搞定了。 Found the answer here . 这里找到答案。 Disclaimer: I've only verified that the database schema is created as expected. 免责声明:我只验证了数据库架构是按预期创建的。 I have not tested that seeding data or further CRUD operations work as expected. 我没有测试播种数据或进一步的CRUD操作按预期工作。

public class Organisation : Customer
{
    [Column(Name = "FinancialContact")]
    public int? FinancialContactId { get; set; }
    [ForeignKey("FinancialContactId")]
    public Person FinancialContact { get; set; }
    [Column(Name = "MainContact")]
    public int? MainContactId { get; set; }
    [ForeignKey("MainContactId")]
    public Person MainContact { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM