Rafa Borges
Rafa Borges

Reputation: 596

Fluent Mapping gone wrong on EF 4.1 with Code First

Here a simple model:

public class Product1
{
   public int Id { get; set; }
   public double Price { get; set; }
   public int CurrencyID  { get; set; }
   public Currency Currency  { get; set; }
}

public class Product2
{
   public int Id { get; set; }
   public double Price { get; set; }
   public int CurrencyID  { get; set; }
   public Currency Currency  { get; set; }
}

public class Currency
{
   public int Id { get; set; }
   public string Name { get; set; }
   public string ISO4217 { get; set; }
   public string Symbol { get; set; }
}

As you can see, Currency is just a list that will be used by two different entities, but If I try to run this, it gives me an error saying that this is not valid as could lead to multiple cascade paths.

Now I'm trying to figure how to model this on OnModelCreating

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<Product1>().HasRequired(p => p.Currency).WithMany().WillCascadeOnDelete(false);
   modelBuilder.Entity<Product2>().HasRequired(p => p.Currency).WithMany().WillCascadeOnDelete(false);
}

But for some reason, although the product is correctly created, whenever I try to load it, Currency comes null.

What am I doing something wrong in this modelling?

Thanks!

Upvotes: 1

Views: 147

Answers (2)

Ehsan
Ehsan

Reputation: 834

Map you classes like this:

public class Product1Mapping : EntityTypeConfiguration<Product1>
    {
     public Product1Mapping ()
      {
       ToTable("Product1");
       HasKey(p => p.Id);
       HasRequired(p => p.Tag).WithMany().HasForeignKey(t => t.CurrencyID);
      }
    }

  public class Product2Mapping : EntityTypeConfiguration<Product2>
    {
      public Product2Mapping ()
      {
       ToTable("Product2");
       HasKey(p => p.Id);
       HasRequired(p => p.Tag).WithMany().HasForeignKey(t => t.CurrencyID);
       //other properties
      }
    }

and change you OnModelCreating creating method like this:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new AccountMapping());
            // Add other mapping classes
        }

       public DbSet<Product1> Product1{ get; set; }
       public DbSet<Product2> Product2{ get; set; }

see these links for more information:

http://msdn.microsoft.com/en-us/data/jj591617.aspx

http://entityframework.codeplex.com/workitem/1049

Upvotes: 0

Rafa Borges
Rafa Borges

Reputation: 596

I figured it out and I will explain here for future reference: After better looking the base created, I realized that it was creating a FK for the wrong field: P1:ID -> Currency:ID, when the correct should be P1:CurrencyID -> Currency:ID

So I found a way to force the correct FK:

modelBuilder.Entity<Product1>().HasRequired(p => p.Currency).WithMany().HasForeignKey(p => p.CurrencyId);

And that's all!

Upvotes: 1

Related Questions