ilhan
ilhan

Reputation: 125

EF Fluent api poco genereting and mapping by using existing Db

I have 2 tables as shown below , I am confused about how I should create poco classes and how can map them.

enter image description here

enter image description here

And i have created the POCO classes as below.

public partial class ExpenseIncome
{
    public int ID { get; set; }
    public int? ExpIncTypeID { get; set; }
    public int AccountID { get; set; }
    public decimal? Amount { get; set; }
    public int? UserID { get; set; }
    public int? GroupID { get; set; }
    public DateTime? AddedDate { get; set; }
    public string Explanation { get; set; }
    public int? ParentItemID { get; set; }
    public decimal? Balance { get; set; }
    public bool? IsActive { get; set; }
    public DateTime? ActivationDate { get; set; }
    // this is  navigation property
    public virtual ExpenseIncomeType ExpenseIncomeType { get; set; }
}

And other POCO is :

public partial class ExpenseIncomeType
{
    public int ID { get; set; }
    public string TName { get; set; }
    public int? IsExpOrInc { get; set; }
    public int? ParentExpTypeID { get; set; }
    public decimal? ExpIncTypeDefaultAmount { get; set; }
    public string UserGroupUniqueValue { get; set; }
    public int? UserGroupID { get; set; }
    // this is  navigation property
    public virtual ExpenseIncome ExpenseIncome { get; set; }
}

So, I am confused about how I can map the these classes in ModelCreating(DbModelBuilder modelBuilder) method of DbContext?

Upvotes: 1

Views: 235

Answers (2)

Gert Arnold
Gert Arnold

Reputation: 109117

This can't be a 1:1 association. Surely, "Many" ExpenseIncomes can share the same ExpenseIncomeType. So the mapping should be:

modelBuilder.Entity<ExpenseIncome>()
            .HasRequired(e => e.ExpenseIncomeType).WithMany();

And the property public virtual ExpenseIncome ExpenseIncome { get; set; } in ExpenseIncomeType should disappear.

It's semantically correct to say that an ExpenseIncome "has a" ExpenseIncomeType. The reverse doesn't make much sense, although technically, ExpenseIncomeType could have an ICollection<ExpenseIncome>.

Upvotes: 1

3615
3615

Reputation: 3875

If you are going for Code First approach, there are 2 ways at least to do this.

  1. in the DbContext you have to override the ModelCreating like you said and write for each entity something like:

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
      { 
        modelBuilder.Entity<ExpenseIncome>().HasKey(e => e.ID);
        modelBuilder.Entity<ExpenseIncomeType>().HasKey(e => e.ID);
      }

  1. Create a mapping class derived from EntityTypeConfiguration and in the constructor of that class indicate related mappings.

    public class ExpenseIncomeMap: EntityTypeConfiguration<ExpenseIncome>
    {
     public RoleConfiguration()
      {
        HasKey(e => e.ID);
        // etc.
      }
    }

And after this each mapping class should be added to context

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ExpenseIncomeMap());
        modelBuilder.Configurations.Add(new ExpenseIncomeTypeMap());
    }

Your Poco models in the DbContext should match exactly the database structure, in this case you will not need to generate any database migration.

Upvotes: 2

Related Questions