2016-10-31 24 views
-1

我用实体的代码生成工具在我的数据库,给我的背景下我该怎么处理这个生成的DbContext?

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated from a template. 
// 
//  Manual changes to this file may cause unexpected behavior in your application. 
//  Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace LRVault.Models 
{ 
    using System; 
    using System.Data.Entity; 
    using System.Data.Entity.Infrastructure; 

    public partial class LRC_VAULTEntities : DbContext 
    { 
     public LRC_VAULTEntities() 
      : base("name=LRC_VAULTEntities") 
     { 
     } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      throw new UnintentionalCodeFirstException(); 
     } 

     public virtual DbSet<Post> Posts { get; set; } 
     public virtual DbSet<Thread> Threads { get; set; } 
    } 
} 

,我希望能够马上使用它像

var context = new LRC_VAULTENtities(); 
context.Posts.InsertOnSubmit(new Post() { ... }); 
context.Posts.SubmitChanges(); 

但是没有Posts属性创建后RC_VAULTENtities的实例。那么我该怎么做?

+2

貌似LRC_VAULTEntities'和'LRC_VAULTENtities'之间'拼写出现错误代码显示,在的DbContext – Nkosi

+0

@Nkosi'存在Posts'财产,不,这不是问题 –

+0

@DeadlyNicotine嗯,有一个错字的地方 - 因为这根本不一定与EF有关。你已经明确声明'Posts'是一个属性,所以你几乎肯定会引用错误的类。 – Rob

回答

0

使用Base类,并添加对项目的引用,以便您可以在任何需要的地方调用dbContext

public class Base 
    { 
     protected internal LRC_VAULTEntities dbContext; 

     public Base() 
     { 
      dbContext = new LRC_VAULTEntities(); 
     } 
    }