2013-03-24 91 views
0

我想创建一个MVC3应用程序,我首先用EF代码来创建数据库。 我有这个表:用户,类别,产品,贷款。 用户可以创建一个或多个类别。 用户可以添加一个或多个产品。 用户可以添加一个或多个贷款。 类别可以有一个或多个产品。 类别属于用户。 产品可以没有或多个贷款。 产品属于用户。 产品属于类别。 贷款属于用户。 贷款被添加到产品。代码第一个数据库模型

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; } 
} 

有无maded背景:

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>(); 
    } 
} 

已添加的connectionString:

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

另外一个已经maded一个简单的初始化器类:

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(); 
    } 
} 

我也有为用户t生成一个控制器o获取用户,但是当我尝试获取用户时,我收到如下错误:

由于EdmMetadata类型未包含在模型中,因此无法检查模型兼容性。确保IncludeMetadataConvention已被添加到DbModelBuilder约定中。

我试图改变Database.SetInitializer<BuisnessContext>(new BuisnessInitializer()); 蒙山Database.SetInitializer<BuisnessContext>(null);

然后我geted该表用户doesen't存在,我没有发现任何表在我APP_DATA文件夹中的错误 - > Buisness.mdf 的数据库已创建,但有任何表。

我知道在我的BuisnessContext中我必须为一对多或类似的东西添加一些代码,但我不知道该怎么做。

请任何帮助!

+0

[EF 4 Code First:由于模型中未包含EdmMetadata类型,无法检查模型兼容性](http://stackoverflow.com/questions/8630755/ef-4-code-first-model -compatibility-不能待检测-因为最edmmetadata-T)。特别是对接受答案的评论。 – 2013-03-24 11:34:34

+0

我在想这不是我的问题。我发现与数据库存在混淆,因为我从用户到更多类别,更多产品的类别和更多产品的用户,这是一个循环,我不能以另一种方式做。所以我只是删除了关系循环和它的工作。如何做到这一点,为我需要的关系工作? – 2013-03-24 12:53:32

+0

我发现的好答案是:http://stackoverflow.com/questions/7367339/net-mvc-cyclical-reference-issue-with-entity – 2013-03-24 15:59:05

回答

0

我发现在.net mvc cyclical reference issue with entity

我的问题的答案所以对我的回答是这样的:

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); 

的原因是,我在这里做像相同的表有些不同路径用户 - 类别,用户 - 类别 - 产品,类别 - 产品,用户 - 产品 - 贷款。

也许这对其他人来说是一个很好的答案。

相关问题