首先在EF代码影响外键列命名(CTP5) [英] Influencing foreign key column naming in EF code first (CTP5)

查看:194
本文介绍了首先在EF代码影响外键列命名(CTP5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与其他类两个单向一元关系的POCO类,这两个类共同的祖先。在生成的模式外键的名称不反映属性名称。 (属性MainContact和FinancialContact给PERSONID和PersonId1字段名)。

I have a POCO class that has two one-way unary relationships with another class, both classes share an ancestor. 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).

我如何能影响模式生成,生成相匹配的属性名称的数据库列名?

How can I influence schema generation to generate database column names that match the property names?

这个模型看起来是这样的:

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

的模式是这样的:
$ b $输入图像描述>

The schema looks like this:

下面druttka的回答做了工作,很高兴知道,这是一个错误CTP5这背后。 EF还需要指定级联行为,我用流利的API来做到这一点下面由druttka给出的链接的例子。一些良好的阅读从莫尔塔扎Manavi的这里

druttka's answer below did the job and it's nice to know that it's a CTP5 bug that's behind this. 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. Some more good reading from Morteza Manavi here.

现在的代码是这样的:

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代码首先使用,默认情况下,约定优于配置。但是,您可以设置通过覆盖DbContent.OnModelCreating明确的替代品。许多例子这里,礼貌ScottGu的。

EF Code First uses, by default, convention over configuration. However, you can set explicit alternatives by overriding DbContent.OnModelCreating. Many examples here, courtesy of ScottGu.

修改

因此,在CTP5, MapSingleType走了描述这里。对于简单的字符串属性下的作品,而不是为你的组织,以人的关系。我很好奇,并计划继续寻找它,但在此期间,也许这将让你开始或其他人可以完成的答案。

So in CTP5, MapSingleType went away as described here. 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; }
}



编辑2

好吧,这得到它。这里找到了答案。免责声明:我只验证按预期的方式创建数据库模式。我没有测试的播种数据或进一步CRUD操作按预期工作。

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.

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

这篇关于首先在EF代码影响外键列命名(CTP5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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