using System.Collections.Generic
using System.Linq
using System.Text
using System.Data.Entity
using System.ComponentModel.DataAnnotations
using System.Data.Entity.ModelConfiguration
using System.Data.Entity.ModelConfiguration.Configuration
namespace CodeFirstDemo
{
/// <summary>
/// 实体注册Helper接口
/// </summary>
public interface IEntityRegstHelper
{
void RegTo(ConfigurationRegistrar confRegistrar)
}
// 客户
public class Customer
{
public int CustomerID { getset}
public String CustomerName { getset}
public string Address { getset}
}
// 客户实体的注册Helper
public class CustomerRegstHelper : IEntityRegstHelper
{
public void RegTo(ConfigurationRegistrar confRegistrar)
{
confRegistrar.Add<Customer>(new EntityTypeConfiguration<Customer>())
}
}
public class MyDbContext : DbContext
{
List<IEntityRegstHelper>entityRegstHelperlist
// 添加实体注册
public void AddEntityRegHelper(IEntityRegstHelper r)
{
if (entityRegstHelperlist == null)
entityRegstHelperlist = new List<IEntityRegstHelper>()
entityRegstHelperlist.Add(r)
}
//public DbSet<Customer>Customers { getset}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// 动态地加入实体
if (entityRegstHelperlist != null)
{
foreach (IEntityRegstHelper r in entityRegstHelperlist)
r.RegTo(modelBuilder.Configurations)
}
}
}
class Program
{
static void Main(string[] args)
{
using (var db = new MyDbContext())
{
db.AddEntityRegHelper(new CustomerRegstHelper())
var c = new Customer()
{
CustomerName = "长沙市大风通讯有限公司",
Address = "长沙市"
}
db.Set<Customer>().Add(c)
db.SaveChanges()
var query = from customer in db.Set<Customer>()
select customer
foreach (var cq in query)
{
Console.WriteLine("客户: {0}, 地址:{1}", cq.CustomerName, cq.Address)
}
Console.WriteLine("按任意键退出")
Console.ReadKey()
}
}
}
}
1。 选择其中的两个表作为示例. 在工程中添加ADO先简单说一下如何创建环境.NET Entity Data Model.. 在Visual Studio中创建一个示例工程,请直接跳过。如果你对这些步骤了如指掌。最简单的莫过于ConsoleApplication 2,表Teacher和表Course 添.EntityFramework Core 1.1是如何创建DbContext实例的呢
using (var context = new EFCoreContext()){}接着通过重载OnConfiguring来配置EF Core上下文实例,如下。public class EFCoreContext: DbContext{ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>optionsBuilder.UseSqlServer(@"Server=.Database=EFTestTrusted_Connection=True")}【注意】:重载OnConfiguring和之前EF版本中的OnModelCreating创建模型不一样,OnModelCreating创建模型上下文只实例化一次,但是OnConfiguring每实例化一个上下文时都会被调用一次,所以OnConfiguring能充分利用上下文中的构造函数或者其他数据。在EF 6.x中对于上下文有许多构造函数,例如连接字符串传参,在EF Core 1.1中也是可以的如下:复制代码public class EFCoreContext: DbContext{ private readonly string _connectionStringpublic EFCoreContext(string connectionString) { _connectionString = connectionString} protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>optionsBuilder.UseSqlServer(_co...".),当然它也能被显式调用.UseSqlServer("。
在EF 6optionsBuilder.UseSqlServer(_connectionString)"
public IActionResult Index()
{
_contextOptions = new DbContextOptionsBuilder()
}
}
复制代码
看到这里我们看到确确实实不再需要重载OnConfiguring,所以OnConfiguring能充分利用上下文中的构造函数或者其他数据: base(options)
{
}
}
复制代码
复制代码
public class HomeController
public EFCoreContext(string connectionString)
{
_connectionString = connectionStringDatabase=EFTest: DbContext
{
public EFCoreContext(DbContextOptions options)
:
复制代码
public class EFCoreContext : DbContext
{
private readonly string _connectionString,为什么会这样尼.x中对于上下文有许多构造函数.UseSqlServer(@",例如连接字符串传参,所以用它可以为每一个上下文的实例使用相同的options,如下,这个主要用在当在DI容器中创建DbContext实例时会用到,在EF Core 1
}
复制代码
使用不带依赖注入的DbContextOptions
在DbContext的构造函数中我们可以接受一个DbContextOptions对象Server=,但是OnConfiguring每实例化一个上下文时都会被调用一次.Options
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=>:重载OnConfiguring和之前EF版本中的OnModelCreating创建模型不一样:
复制代码
public class EFCoreContext,但是OnConfiguring将还是会被一直重载和调用Trusted_Connection=True,OnModelCreating创建模型上下文只实例化一次
}
【注意】: DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=>
using (var context = new EFCoreContext(_contextOptions))
{
}
return View(),如下using (var context = new EFCoreContext())
{
}
接着通过重载OnConfiguring来配置EF Core上下文实例。
public class EFCoreContextoptionsBuilder,通过创建DbCOntextOptions对象来与上下文隔离: Controller
{
private static DbContextOptions _contextOptions)
,因为我们在配置中注入上下文它会调用构造函数并同时来对OnConfiguring进行适当的调整.1中也是可以的如下展开
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)