0

I have created a News Model. My News have an author which is of the Member Class. is this enough for the foreing key to be set?

 HasRequired(n => n.Author);

Code:

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



    }
}
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283

1 Answers1

0

Thanks @Gert for the answer:

It is not enough. It is too much. Well, strictly speaking. The FK will even be set (as in generated in the database's data model) without the HasRequired. With it, the FK will be not nullable.

Community
  • 1
  • 1
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283