Entity Framework Code First creates classes from multiple tables in ASP.Net

yogeshc
 
on Sep 20, 2021 01:05 AM
430 Views

Hello Sir,

Create model class for multiple tables in code first entity framework

i wants to create Four tables in my DB as follows

1. Language => Id, Name, etc

2. Category => Id, Name, Language_id, etc

3. Products=> id , name, Category_Id, Language_Id, ...

4. Page_Content => Id, Page_Name, Language _Id

and i wants  to do CRUD oprations with all above tables

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 20, 2021 04:32 AM

Hi yogeshc,

Refer below class.

Namespace

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

Model

public class Language
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Category
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    [NotMapped]
    [ForeignKey("Language")]
    public int Language_Id { get; set; }

    public virtual Language Language { get; set; }
}

public class Product
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    [ForeignKey("Language")]
    public int Language_Id { get; set; }
    [ForeignKey("Category")]
    public int Category_Id { get; set; }

    public virtual Language Language { get; set; }
    public virtual Category Category { get; set; }
}

public class Page_Content
{
    [Key]
    public int Id { get; set; }
    public string Page_Name { get; set; }

    [ForeignKey("Language")]
    public int Language_Id { get; set; }

    public virtual Language Language { get; set; }
}