在将navagation属性用于复杂类型时,使用可插入约定来更改外键的默认属性名称。 [英] Using plugable conventions to change the default property name of a foreign key when using a navagation property to a complex type.

查看:36
本文介绍了在将navagation属性用于复杂类型时,使用可插入约定来更改外键的默认属性名称。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


 

 

 

我有一个现有的数据库我想用
和Entity Framework Code First。 基于这个文章( http://blogs.msdn.com/b/adonet/archive/2011/01/10/ef-feature-ctp5-pluggable-conventions.aspx?wa=wsignin1.0
I我正在尝试更改默认约定。 我已成功更改它以使用单数表名称,但现在想要调整导航属性的行为方式。

< span style ="font-family:Consolas;颜色:#0000ff; font-size:x-small"> < span style ="font-family:Consolas; color:#0000ff; font-size:x-small">看下面的例子,我想能够
在数据库中有一个名为'HomeAddressId'的字段这是Person表中的外键到Address表中的主键。 如果我添加名为HomeAddressId的主要属性,它可以工作,但我希望能够删除它。 删除
后,它将采用HomeAddressAddressID的名称。 

如何更改约定以使用PropertyName +" ID"?

How can I change the conventions to use the PropertyName + "ID"?

感谢您的帮助......

Thank you for your help...

public
class
< span style ="font-family:Consolas;颜色:#2b91af; font-size:x-small"> 人员<
{

   public
int
PersonId {
get ;
set ;
}

   public
string
FirstName {
get ;
set ;
}

   public
string
LastName {
get ;
set ;
}

   public
虚拟
地址
HomeAddress {
get ;
set ;
}

}

public class Person
{
   public int PersonId { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public virtual Address HomeAddress { get; set; }
}

 

 

public
class
地址

{

  
public
int
AddressId {
get ;
set ;}

  
public
string
地址1 {
get ;
set ;
}

  
public
string
City {
get ;
set ;
}

  
public
string
状态{
get ;
set ;
}

  
public
string
Zip {
get ;
set ;
}

}

public class Address
{
  
public int AddressId { get; set;}
  
public string Address1 { get; set; }
  
public string City { get; set; }
  
public string State { get; set; }
  
public string Zip { get; set; }
}

推荐答案

Hi William,

Hi William,

这在CTP5中预览的约定功能中是不可能的(关系的配置仍然是内部的。)而不是使用约定,你可以编写一些代码来反映您的类型,查找引用属性并使用
反射来调用Fluent API。由于Fluent API中的所有通用方法,它将是混乱的代码,但它应该是可能的。

This isn't possible in the convention functionality that was previewed in CTP5 (configuration of relationships was still internal). Rather than using conventions, you could write some code to reflect over your types, look for reference properties and use reflection to call the Fluent API. It will be messy code because of all the generic methods in the Fluent API, but it should be possible.

~Rowan


这篇关于在将navagation属性用于复杂类型时,使用可插入约定来更改外键的默认属性名称。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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