Entity Framework 4.3 - 使用 DataContext 接口配置 ConnectionString
我将 EF 4.3 与 Ninject 结合使用。我有一个简单的 DataContext,并为其创建了一个接口。这是一个简单的接口:
public interface IMyDataContext
{
DbSet<ComplexType> ComplexTypes { get; set; }
int SaveChanges();
}
MyDataContext 的实现:
public class MyDataContext : DbContext, IMyDataContext
{
public DbSet<ComplexType> ComplexTypes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); //Not sure if this is necessary..
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
我将其添加到 NInject:
Kernel.Bind<IMyDataContext>().To<MyDataContext>();
使用连接字符串名称 MyDataContext 不起作用,如果我使用 IMyDataContext,它会生成另一个名为完整程序集名称的数据库。仅映射了一个表/类..不确定这是否与之有关。
<connectionStrings>
<add name="MyDataContext"
connectionString="Server=MyPc\SQLEXPRESS;Database=MyDataContext;Persist Security Info=True;Integrated Security=true;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
我知道我可以重写数据上下文上的构造函数......但我认为这应该是“自动映射”。有什么想法吗?
提前致谢!
I'm using EF 4.3 with Ninject. I have a simple DataContext and I've created an interface for it. It's a simple interface:
public interface IMyDataContext
{
DbSet<ComplexType> ComplexTypes { get; set; }
int SaveChanges();
}
Implementation of MyDataContext:
public class MyDataContext : DbContext, IMyDataContext
{
public DbSet<ComplexType> ComplexTypes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); //Not sure if this is necessary..
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
And I'm adding it to NInject:
Kernel.Bind<IMyDataContext>().To<MyDataContext>();
Using the connection string name MyDataContext doesn't work and if I use IMyDataContext, it generates another database named the full assembly name. Only one table/class is mapped.. not sure if that has anything to do with it.
<connectionStrings>
<add name="MyDataContext"
connectionString="Server=MyPc\SQLEXPRESS;Database=MyDataContext;Persist Security Info=True;Integrated Security=true;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
I know I can override the constructor on the data context.... but I thought this was supposed to "auto map". Any thoughts?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要做一个
与此结合:
以避免任何问题。
此外,映射的内容取决于您的 DbSet<>在 DbContext 上声明时,您还应该发布 MyDataContext 实现。
Just do a
In conjunction with this:
To avoid any problem.
Also what is mapped depends on your DbSet<> declaration on your DbContext, you should also post the MyDataContext implementation.