2014-11-04 78 views
0

嗨,我想知道有谁可以看到我做错了什么。我tryng使用实体框架,然后种子创建数据库 但不断收到错误为什么我的创业框架数据库不能工作

模型 - 这是在我所创建的类A和B的模式,都有一个FK 我使用的DbContext尝试创建数据库

 namespace MvcAb.Models 
{ 
    public class AaSeed : DropCreateDatabaseAlways<AaDb> 
    { 
     protected override void Seed(AaDb context) 
     { 
      context.Aas.Add(new Aa() 
       { 
        AaName="Name", 
        AaDesc="Big Description", 
        Bbs = new List<Bb>() 
        { 
         new Bb() 
         { 
          BbName="This Name", 
          BbTitle="This title" 
         }//end new 
        }//end list 
       });// end new Aa 

       context.Aas.Add(new Aa() 
       { 
        AaName="name two", 
        AaDesc="Second Desc" 
       }); 

      context.SaveChanges(); 
     } 
    } 

    public class AaDb : DbContext 
    { 
     public DbSet<Aa> Aas { get; set; } 
     public DbSet<Bb> Bbs { get; set; } 

     public AaDb() 
      : base("AaDb") 
     { 

     } 
    } 


    public class Aa 
    { 
     public int AaId { get; set; } 
     public string AaName { get; set; } 
     public string AaDesc { get; set; } 
     public List<Bb> Bbs { get; set; } 
    } 

    public class Bb 
    { 
     public int BbId { get; set; } 
     public string BbName { get; set; } 
     public string BbTitle { get; set; } 
     public Aa Aa { get; set; } 
    } 
} 

控制器 - 这是控制器的页面创建一个新的数据库连接,然后添加到将在我的视图中显示被列表

private AaDb Db = new AaDb(); 

     // 
     // GET: /Home/ 

     public ActionResult Index() 
     { 
      return View(Db.Aas.ToList()); 
     } 

     // 
     // GET: /Home/Details/5 

     public ActionResult Details(int id) 
     { 
      return View(Db.Aas.Find(id)); 
     } 

指数 - 这是我的索引它在哪里乌尔德显示表和数据应在

@model IEnumerable<MvcAb.Models.Aa> 
    <div class="jumbotron"> 
     <h1>Table of Data</h1> 
     <p> 
      @* @Html.ActionLink("Create New", "Create")*@ 
     </p> 
     <table class="table table-bordered"> 
      <tr> 
       <th> 
        Id 
       </th> 
       <th> 
        @Html.DisplayNameFor(model => model.AaName) 
       </th> 
       <th> 
        @Html.DisplayNameFor(model => model.AaDesc) 
       </th> 
       <th></th> 
      </tr> 

      @foreach (var item in Model) 
      { 
       <tr> 
        <td> 
         @Html.DisplayFor(modelItem => item.AaId) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => item.AaName) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => item.AaDesc) 
        </td> 
        <td>      
        @Html.ActionLink("Details", "Details", new { id = item.AaId }) 
        </td> 
       </tr> 
      } 

     </table> 
    </div> 
+0

你需要证明你得到 – 2014-11-04 20:22:51

+2

显示错误将是非常有益的错误,我会种子的方式。此外,你显示你的种子代码,这意味着问题出现在更新数据库中,但你也显示控制器和索引代码。您的数据库是否已创建并填充了您期望的数据? – Shriike 2014-11-04 20:23:50

回答

1

阅读确保去Global.asax中,并把这个作为我没有看到你的代码,它

Database.SetInitializer(new BlogSeed()); 

你编辑连接字符串?

add name="AaDb" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;I nitial Catalog=BlogDb;Integrated Security=SSPI;" 
0

这是进入瓦尔

var Bb = new List<Bb>() 
     { 
      new Bb() {}, 
     }; 
     //movie list var 
     var Aa = new List<Aa>() 
     { 
      new Aa() {     
      } 
     }; 
     //save movie 
     Bb.ForEach(a => context.Movie.Add(a)); 
     context.SaveChanges(); 
相关问题