具有流畅实体框架的外键

时间:2012-09-27 13:46:50

标签: entity-framework fluent

我创建了一个新闻模型。我的新闻有一个成员类的作者。  这足以让外键设置好吗?

 HasRequired(n => n.Author);

代码:

public class Member : Identity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public Address Address { get; set; }
    public int Id { get; set; }

    public virtual ICollection<News> News { get; set; }

}

public class News
{
    public string Title { get; set; }
    public string Subtile { get; set; }
    public int Id { get; set; }
    public string Url { get; set; }
    public DateTime DateAdded { get; set; }


    public virtual Member Author;

}
public class NewsMap : EntityTypeConfiguration<News>
{
    public NewsMap()
    {

        HasKey(n => n.Id);
        Property(n => n.Id).
            HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("Id");

        Property(t => t.Title)
            .IsRequired()
            .HasMaxLength(100)
            .IsUnicode();
        Property(t => t.Subtile)
            .IsRequired()
            .HasMaxLength(100)
            .IsUnicode();
        Property(t => t.Url)
            .IsRequired()
            .HasMaxLength(255)
            .IsUnicode();
        Property(t => t.DateAdded).HasColumnName("DateAdded")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

        HasRequired(n => n.Author); //is this enough for the foreing key to be set?



    }

}
public class MemberMap : EntityTypeConfiguration<Member>
{
    public MemberMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Properties
        this.Property(t => t.FirstName)
            .IsRequired()
            .HasMaxLength(150);

        this.Property(t => t.LastName)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.Email)
            .IsRequired()
            .HasMaxLength(150);

        this.Property(t => t.Address.Id)
            .IsRequired()
            .HasMaxLength(100)
            .HasColumnName("Address_FirstLine");

        this.Property(t => t.Address.ZipCode)
            .IsRequired()
            .HasMaxLength(20).HasColumnName("Address_Zip");

        this.Property(t => t.Address.Contry)
            .IsRequired()
            .HasMaxLength(100).HasColumnName("Address_Contry");

        this.Property(t => t.Address.City)
            .IsRequired()
            .HasMaxLength(100).HasColumnName("Address_Town");

        this.Property(t => t.Id);


        // Table & Column Mappings
        this.ToTable("Identities_Member");
        this.Property(t => t.FirstName).HasColumnName("FirstName");
        this.Property(t => t.LastName).HasColumnName("LastName");
        this.Property(t => t.Email).HasColumnName("Email");
        this.Property(t => t.Id).HasColumnName("Id");

        // Relationships



    }
}

1 个答案:

答案 0 :(得分:0)

感谢@Gert for the answer

  

这还不够。实在是太多了。好吧,严格来说。甚至可以在没有HasRequired的情况下设置FK(如在数据库的数据模型中生成的那样)。有了它,FK将不可为空。