Hilarioc
Hilarioc

Reputation: 13

Foreign key C# MVC 5?

I would like to relate a string field as a foreign key, I need help please!

catalogtype.code => catalog.typeCatalogRefCode

public class CatalogType : AuditFields
{
    [Key]
    public int id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string code { get; set; }
    public bool passive { get; set; }
    public ICollection<Catalog> Catalogs { get; set; }

}

public class Catalog : AuditFields
{
    [Key]
    public int id { get; set; }
    public string value { get; set; }
    public string description { get; set; }
    public string code { get; set; }
    public bool passive { get; set; }
    public int order { get; set; }
    public string typeCatalogRefCode { get; set; }
    public virtual CatalogType catalogType { get; set; }
}

.........................................................................

public class AuditFields
{
    public DateTime createdDate { get; set; }
    public string createdBy { get; set; }
    public string createdIn { get; set; }
    public DateTime modifiedDate { get; set; }
    public string modifiedBy { get; set; }
    public string modifiedIn { get; set; }
}

Upvotes: 0

Views: 797

Answers (3)

Koderzzzz
Koderzzzz

Reputation: 869

Please refer the following class structure. For making "typeCatalogRefCode" as foreign key you need to define "code" as key

class MyDBContext:DbContext
{
    public MyDBContext()
        : base("mydbcontext")
    {

    }
    static MyDBContext()
{
    Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyDBContext>());
}

public DbSet<CatalogType> CatalogTypes{ get; set; }
public DbSet<Catalog> Catalogs{ get; set; }
}
public class CatalogType : AuditFields
{

    public int id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
            [Key]
    public string code { get; set; }
    public bool passive { get; set; }
    public ICollection<Catalog> Catalogs { get; set; }

}

public class Catalog : AuditFields
{
    [Key]
    public int id { get; set; }
    public string value { get; set; }
    public string description { get; set; }
    public string code { get; set; }
    public bool passive { get; set; }
    public int order { get; set; }
    public string typeCatalogRefCode { get; set; }
      [ForeignKey("typeCatalogRefCode")]
    public virtual CatalogType catalogType { get; set; }
}
public class AuditFields
{
    public DateTime createdDate { get; set; }
    public string createdBy { get; set; }
    public string createdIn { get; set; }
    public DateTime modifiedDate { get; set; }
    public string modifiedBy { get; set; }
    public string modifiedIn { get; set; }
}

Upvotes: 1

B&#233;ranger
B&#233;ranger

Reputation: 673

Koderzzzz answer is right but just to complete you can also use data annotation :

public class CatalogType : AuditFields
{
    [Key]
    public int id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string code { get; set; }
    public bool passive { get; set; }
    [InverseProperty("catalogType")]
    public ICollection<Catalog> Catalogs { get; set; }

}

public class Catalog : AuditFields
{
    [Key]
    public int id { get; set; }
    public string value { get; set; }
    public string description { get; set; }
    public string code { get; set; }
    public bool passive { get; set; }
    public int order { get; set; }
    public string typeCatalogRefCode { get; set; }
    [ForeignKey("typeCatalogRefCode")]
    public virtual CatalogType catalogType { get; set; }
}

Upvotes: 0

Koderzzzz
Koderzzzz

Reputation: 869

Hi for this you need to write code on override method OnModelCreating of your dbcontext class. Please refer the following code.

public class CatalogContext : DbContext
{
    static CatalogContext()
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CatalogContext>());
    }
    public DbSet<CatalogType> CatalogTypes{ get; set; }
    public DbSet<Catalog> Catalogs{ get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelbuilder.Entity<Catalog>().HasOptional(a=>a.catalogType)
                            .WithMany(au=>au.Catalogs)
                            .HasForeignKey(a=>typeCatalogRefCode);
    }
}

Upvotes: 1

Related Questions