Anc*_*ent 16 c# entity-framework entity-framework-4.1 entity-framework-5
我刚刚开始制作EntityTypeConfiguration类并做了以下操作
public class Xyz
{
public int PlaceId { get; set; }
public string Name { get; set; }
public DbGeography Location { get; set; }
public int HumanTypeId { get; set; }
public int AddressId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并在EntityTypeConfiguration类中
public sealed class XyzConfiguration:EntityTypeConfiguration<Xyz>
{
public XyzConfiguration()
{
ToTable("Place", "dbo");
HasKey(p => p.PlaceId);
Property(p => p.PlaceId)
.HasColumnName("PlaceId")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.Name);
Property(p => p.Location). ;
Property(p => p.HumanTypeId);
Property(p => p.AddressId);
}
}
Run Code Online (Sandbox Code Playgroud)
现在如何设置DbGeography和外键列HumanTypeId , AddressId?
提前致谢
Chr*_*ris 32
这取决于你要对列做什么.如果您有类似的外键列AddressId,则可能有一些Address要与Xyz实体关联的实体.您需要确定entites如何相互关联,并配置它们之间所需的映射.
您将需要在您的Address类或您的Xyz类中使用导航属性,否则没有任何东西可以将外键绑定到,并且您的外部ID列将被视为普通列(这很好,如果这是您想要的).
因此,如果您要向您的Xyz实体添加导航属性
public class Xyz
{
// Your code
public int AddressId { get; set; }
public virtual Address MyAddress { get; set; }
}
// Your Address class
public class Address
{
public int ID;
}
Run Code Online (Sandbox Code Playgroud)
您可以通过沿着这些行执行某些操作来配置映射(它将根据关系而变化:
public sealed class XyzConfiguration : EntityTypeConfiguration<Xyz>
{
public XyzConfiguration()
{
// Your code.
this.HasOptional(x => x.MyAddress) // Your Xyz has an optional Address
.WithMany() // Address may be owned by many Xyz objects
.HasForeignKey(x => x.AddressId); // Use this foreign key.
}
}
Run Code Online (Sandbox Code Playgroud)
我没有尝试使用空间类型和EF,但我从这里开始:http://msdn.microsoft.com/en-us/data/hh859721.aspx
有关EF页面入门的映射配置的大量信息:http://msdn.microsoft.com/en-us/data/ee712907尝试"Fluent API - 配置/映射属性和类型"
这里对不同的关联类型也有一个略微简化的解释: 代码优先:独立关联与外键关联?
| 归档时间: |
|
| 查看次数: |
22284 次 |
| 最近记录: |