Entity Framework 4.1Code First 连接到 Sql Server 2005
我正在尝试将 Entity Framework 4.1 RC 与 SQL Server 2005 实例一起使用。我创建了一个空数据库,我想将 POCO 对象保存到其中。我的 POCO 看起来像:
public class Cart
{
public Cart()
{
this.CartId = Guid.NewGuid();
}
public Guid CartId { get; set; }
public decimal TotalCost { get; set; }
public decimal SubTotalCost { get; set; }
public decimal Tax { get; set; }
public decimal EstimatedShippingCost { get; set; }
}
我的 CartContext 是:
public class CartContext : DbContext
{
public DbSet<Cart> Carts { get; set; }
public DbSet<Attribute> Attributes { get; set; }
public DbSet<AttributeItem> AttributeItems { get; set; }
}
我有一个连接字符串:
<add name="CartContext" connectionString="Server=myserver.mynetwork.net;User ID=MyUser;Pwd=mypassword;Initial Catalog=ExistingDb" providerName="System.Data.SqlClient" \>
当我尝试将对象添加到上下文并保存它时,我得到:
System.Data.Entity.Infrastruct.DbUpdateException: 更新时发生错误 条目。请参阅内部异常 细节。 ---> System.Data.UpdateException:错误 更新条目时发生。 有关详细信息,请参阅内部异常。 ---> System.Data.SqlClient.SqlException: 对象名称“dbo.Carts”无效。
如果我分析数据库,我可以看到用户连接,在 sys.tables 中查找数据库,运行此查询:
SELECT TOP (1)
[Extent1].[Id] AS [Id],
[Extent1].[ModelHash] AS [ModelHash]
FROM [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC
然后尝试插入我的购物车对象。它从不尝试创建 Carts 表。我猜连接字符串有问题,但我无法在任何地方找到有关如何执行此操作的示例。
I'm trying to use the Entity Framework 4.1 RC with a SQL Server 2005 instance. I've created an empty database and I'd like to persist my POCO objects to it. My POCO looks like:
public class Cart
{
public Cart()
{
this.CartId = Guid.NewGuid();
}
public Guid CartId { get; set; }
public decimal TotalCost { get; set; }
public decimal SubTotalCost { get; set; }
public decimal Tax { get; set; }
public decimal EstimatedShippingCost { get; set; }
}
My CartContext is:
public class CartContext : DbContext
{
public DbSet<Cart> Carts { get; set; }
public DbSet<Attribute> Attributes { get; set; }
public DbSet<AttributeItem> AttributeItems { get; set; }
}
I have a connection string:
<add name="CartContext" connectionString="Server=myserver.mynetwork.net;User ID=MyUser;Pwd=mypassword;Initial Catalog=ExistingDb" providerName="System.Data.SqlClient" \>
When I try and add an object to the context and save it I get:
System.Data.Entity.Infrastructure.DbUpdateException:
An error occurred while updating the
entries. See the inner exception for
details. --->
System.Data.UpdateException: An error
occurred while updating the entries.
See the inner exception for details.
---> System.Data.SqlClient.SqlException:
Invalid object name 'dbo.Carts'.
If I profile the database I can see the user connect, look for the database in sys.tables run this query:
SELECT TOP (1)
[Extent1].[Id] AS [Id],
[Extent1].[ModelHash] AS [ModelHash]
FROM [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC
Then attempt to insert my cart object. It never tries to create the Carts table. I'm guessing there's something wrong with the connection string, but I can't find examples anywhere on how to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DbContext 不会因为表不存在而创建它。使用现有数据库后,您还必须手动创建表或创建自定义初始值设定项。默认初始化程序只能删除数据库并使用所有必需的表创建新数据库。
例如,您可以致电:
或:
或:
DbContext will not create table just because it doesn't exists. Once you are using existing database you must also manually create tables or create custom initializer. Default initializers are only able to drop the database and create new one with all required tables.
You can for example call:
Or:
Or: