我应该用这个生成的DbContext做什么?

时间:2016-10-31 02:14:43

标签: c# asp.net asp.net-mvc entity-framework linq

我在我的数据库中使用了Entity的代码生成工具,它给了我上下文

//------------------------------------------------------------------------------
// <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属性。那我该怎么办?

1 个答案:

答案 0 :(得分:0)

使用Base课程并添加对项目的引用,以便您可以随时随地拨打dbContext

public class Base
    {
        protected internal LRC_VAULTEntities dbContext;

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