EF代码第一 - 复合键 [英] EF code first - composite key

查看:155
本文介绍了EF代码第一 - 复合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个collumns旧的数据库,我想将它们映射为1 ID这是可能的。

I have a legacy database which has two collumns and I want to map them as 1 ID is this possible

例如:

    public class Product
{ 
public string ProductID {get;set;}
public string ShortDescription {get;set;}
public string UserName {get;set;}
}

然后我ModelBinder的长相像这样

then my Modelbinder looks like this

modelBinder.Entity<Product>().
HasKey(p=>p.ProductID)
.MapSingle(product =>
new {
 colShotDesc = product.ShortDescription,
 colUser = product.UserName
}
).ToTable("Products");



我需要什么会像
的ProductID = SHORTDESCRIPTION +用户名在映射..
,因为这两个collumns共享独特的键约束实现的...

What I need would be something like ProductID = ShortDescription + UserName in the mapping... because these two collumns share a unique key contraint...

不知道这使得SENS但是任何建议将是巨大...
请不要问有关数据库设计=>这是喜欢它,不应该改变...这就是为什么我认为EF代码优先可以帮我(希望横指)...
因为它看起来像DB还没有得到确定的PK只是唯一键约束......

Don't know if this makes sens but any suggestions would be great... Please don't ask about the database design => this is like it is and should not be changed... that's why I thought EF code-first could help me (hope cross fingers)... because it looks like the db hasn't got pk defined just unique key constraints...

反正...
帮助将是很棒的..

anyway ... help would be terrific..

推荐答案

听起来像你想的复杂类型,并且希望在复杂类型通过使用在附加标识的惯例被视为关键属性名结束。

Sounds like you want a complex type AND want the complex type to be recognized as the key by using the convention of appending ID on the end of the property name.

EF CF不能这样做,在这一点上。

EF CF can't do that at this point.

您可以告诉通过关键属性或FluentAPI复合键EF CF

You can tell EF CF about a composite key through the Key attribute or FluentAPI.

数据注释:

public class Product 
{  
  [Key, Column(Order=0)]
  public string ShortDescription {get;set;} 
  [Key, Column(Order=1)]
  public string UserName {get;set;} 
}

流利的API:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.Entity<Product>()
               .HasKey(p=> new{p.ShortDescription, p.UserName});
}

您可以创建一个复杂的类型,你可以在你的代码中使用的工作更你希望你的代码从概念上的工作方式。

You can create a complex type that you can use in your code to work more the way you want your code to work conceptually.

public class Product 
{  
  public ProductID Key {get;set;}
}

public class ProductID 
{
  public string ShortDescription {get;set;} 
  public string UserName {get;set;} 
}

然后用流利的API来映射它:

Then to map it using the Fluent API:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.ComplexType<ProductID>()
               .Property(p=>p.ShortDescription)
               .HasColumnName("ShortDescription")
               .Property(p=>p.UserName)
               .HasColumnName("UserName");
}



或者,如果你想使用数据注释:

Or if you want to use Data Annotations:

[ComplexType]
public class ProductID
{
  [Column("ShortDescription")]
  public string ShortDescription {get;set;} 
  [Column("UserName")]
  public string UserName {get;set;} 
}

您必须指定列名或配置要承担列名ProductID_ShortDescription ....

You have to specify the column names or the configuration is going to assume the column names are ProductID_ShortDescription....

这里有复杂类型的一些详细信息。

这篇关于EF代码第一 - 复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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