C# 如何在 EntityTypeConfiguration 类中设置外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18809111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:15:58  来源:igfitidea点击:

How to set foreign key in EntityTypeConfiguration Class

c#entity-frameworkentity-framework-5entity-framework-4.1

提问by Ancient

I just started to make EntityTypeConfiguration class and did following

我刚开始制作 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; }
}

and in EntityTypeConfiguration class

并在 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);
    }
}

Now how to set DbGeographyand foreign key columns HumanTypeId , AddressId?

现在如何设置DbGeography外键列HumanTypeId , AddressId

Thanks in advance

提前致谢

采纳答案by Chris

It depends on what you're going to do with the columns. If you have foreign key columns like AddressId, you probably have some Addressentities that you want to relate to your Xyzentities. You need to decide how the entites relate to each other, and configure the mapping you want between them.

这取决于您将如何处理这些列。如果您有像 一样的外键列AddressId,那么您可能有一些Address想要与您的Xyz实体相关联的实体。您需要决定实体如何相互关联,并在它们之间配置您想要的映射。

You will need a navigation property either in your Addressclass, or your Xyzclass, otherwise there isn't anything to bind the foreign key to, and your foreign ID columns would just be treated as normal columns (which is fine, if that's what you want).

您将需要在您的Address班级或Xyz班级中使用导航属性,否则没有任何可以绑定外键的内容,并且您的外键 ID 列将被视为普通列(这很好,如果这是您想要的) )。

So, if your were to add a navigation property to your Xyzentity

因此,如果您要向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;
}

You could configure the mapping by doing something along these lines (it will vary depending on the relationship:

您可以通过执行以下操作来配置映射(它会因关系而异:

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

I haven't tried using spatial types and EF, but I'd start here: http://msdn.microsoft.com/en-us/data/hh859721.aspx

我还没有尝试使用空间类型和 EF,但我会从这里开始:http: //msdn.microsoft.com/en-us/data/hh859721.aspx

There's a wealth of information on mapping configurations on the getting started with EF pages: http://msdn.microsoft.com/en-us/data/ee712907try "Fluent API - Configuring/Mapping Properties & Types"

EF 页面上有大量关于映射配置的信息:http: //msdn.microsoft.com/en-us/data/ee712907尝试“Fluent API - 配置/映射属性和类型”

There's also a slightly abridged explanation of the different association types here: Code First: Independent associations vs. Foreign key associations?

这里还有对不同关联类型的稍微删节的解释: 代码优先:独立关联与外键关联?