0

I'm trying to create a MVC3 application, i'm troubled with EF code first to create DB. I have this tables: User, Category, Product, Loan. A User can create none or more Categories. A User can add none or more Products. A User can add none or more Loans. A Category can have one or more Products. A Category belongs to a User. A Product can have none or more Loans. A Product belongs to a User. A Product is in a Category. A Loan belongs to a User. A Loan is added to a Product.

public class User
{
    public int UserID { get; set; }
    public string UserName { get; set; }

    public virtual ICollection<Category> Categorys { get; set; }
    public virtual ICollection<Product> Products { get; set; }
    public virtual ICollection<Loan> Loans { get; set; }
}
public class Category
{
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }

    public int UserID { get; set; }

    public virtual User User { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}
    public class Product
{
    public int ProductID { get; set; }
    public string ProductName { get; set; }

    public int UserID { get; set; }
    public int CategoryID { get; set; }

    public virtual User User { get; set; }
    public virtual Category Category { get; set; }
    public virtual ICollection<Loan> Loans { get; set; }
}
    public class Loan
{
    public int LoanID { get; set; }
    public bool LoanStatus { get; set; }

    public int UserID { get; set; }
    public int ProductID { get; set; }

    public virtual User User { get; set; }
    public virtual Product Product { get; set; }
}

Have maded the context:

public class BuisnessContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Category> Categorys { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<Loan> Loans { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

Have added the connectionString:

<add name="BuisnessContext"
     connectionString="Data Source=|DataDirectory|Buisness.sdf"
     providerName="System.Data.SqlServerCe.4.0"/>

Also a have maded a simple Initializer class:

public class BuisnessInitializer : DropCreateDatabaseIfModelChanges<BuisnessContext>
{
    protected override void Seed(BuisnessContext context)
    {
        var users = new List<User>
        {
            new User { UserName = "u1"},
            new User { UserName = "u2"}            };
        users.ForEach(s => context.Users.Add(s));
        context.SaveChanges();

        var categories = new List<Category>
        {
            new Category { CategoryName = "N1", UserID=1 }            };
        categories.ForEach(s => context.Categorys.Add(s));
        context.SaveChanges();

        var products = new List<Product>
        {
            new Product { ProductName = "N1", UserID = 1, CategoryID = 1 }
        };
        products.ForEach(s => context.Products.Add(s));
        context.SaveChanges();

        var loans = new List<Loan>
        {
            new Loan { LoanStatus = true, UserID = 2, ProductID = 1 }
        };
        loans.ForEach(s => context.Loans.Add(s));
        context.SaveChanges();
    }
}

Also i have generate a controller for User to get the users, but when i try to get the Users i received an error like:

Model compatibility cannot be checked because the EdmMetadata type was not included in the model. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.

I tried to change the Database.SetInitializer<BuisnessContext>(new BuisnessInitializer()); whith Database.SetInitializer<BuisnessContext>(null);

Then i geted the error that table User doesen't exist and i didn't find any table in my APP_DATA folder -> Buisness.mdf The database was created, but there was any table.

I understand that in my BuisnessContext i must to put some code for One to many or something like this, but i don't know how to do that.

Any help please!

Morar Mihai
  • 77
  • 1
  • 2
  • 7
  • possible duplicate of [EF 4 Code First: Model compatibility cannot be checked because the EdmMetadata type was not included in the model](http://stackoverflow.com/questions/8630755/ef-4-code-first-model-compatibility-cannot-be-checked-because-the-edmmetadata-t). Especially the comment on the accepted answer. – Gert Arnold Mar 24 '13 at 11:34
  • i'm thinking that this is not the problem i have. I was founding that there are confusion with DB, because i have relation from a User to more Categories, a Category to more Products and a User to more Products, this is a cycle and i can't do another way. So i just removed the relations cycling and it works. How to do this to work for my relations that i need? – Morar Mihai Mar 24 '13 at 12:53
  • the good answer for me that i found is: http://stackoverflow.com/questions/7367339/net-mvc-cyclical-reference-issue-with-entity – Morar Mihai Mar 24 '13 at 15:59

1 Answers1

0

I have found the answer for my question on .net mvc cyclical reference issue with entity

So the answer for me is like this:

modelBuilder.Entity<Product>()
            .HasRequired(p => p.User).WithMany(p => p.Products).HasForeignKey(p => p.UserID).WillCascadeOnDelete(false);
        modelBuilder.Entity<Product>()
            .HasRequired(p => p.Category).WithMany(p => p.Products).HasForeignKey(p => p.CategoryID).WillCascadeOnDelete(false);
        modelBuilder.Entity<Loan>()
            .HasRequired(l => l.Product).WithMany(l => l.Loans).HasForeignKey(l => l.ProductID).WillCascadeOnDelete(false);

The reason is that here i am making some different path for the same tables like User - Category, User - Category - Product, Category - Product, User - Product - Loan.

Maybe it will be a good answer for others.

Community
  • 1
  • 1
Morar Mihai
  • 77
  • 1
  • 2
  • 7