Entity framework 实体框架,连接字符串引发formatexception
我使用的是实体框架6.1.1 我在一个项目中有一个类,其中包括FBuyShopContext,另一个用于模型,还有一个带有MVC5的Asp项目 在我的第一个项目中,我有以下几点Entity framework 实体框架,连接字符串引发formatexception,entity-framework,connection-string,Entity Framework,Connection String,我使用的是实体框架6.1.1 我在一个项目中有一个类,其中包括FBuyShopContext,另一个用于模型,还有一个带有MVC5的Asp项目 在我的第一个项目中,我有以下几点 public class FBuyShopContext : DbContext { public FBuyShopContext() : base("name=FBuyShopContext") { } public virtual DbSet<Product&
public class FBuyShopContext : DbContext
{
public FBuyShopContext()
: base("name=FBuyShopContext")
{
}
public virtual DbSet<Product> Admins { get; set; }
}
我的连接字符串如下
<connectionStrings>
<add name="FBuyShopContext" connectionString="data source=tschikovani\SQLEXPRESS;initial catalog=OnlineShop;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
我有一个例外
An exception of type 'System.ArgumentException' occurred in EntityFramework.dll
keyword not supported: 'data source'.
为什么会这样?连接字符串完全正确我可以看到这里发生了三件事: 1 DbContext的构造函数应采用连接字符串的名称,因此将其更改为仅FBuyShopContext,而不是name=FBuyShopContext 2您的连接字符串以a结尾,这是一个无效字符。你应该移除它 3应使用fluent API或通过数据注释在模型上指定主键:
public class Product {
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
我很确定您所收到的错误是由2引起的,但是1和3会在以后给您带来问题。我删除了,但没有任何更改,我有一个例外still@needermen是同一个例外吗?您是否也修复了1和3?是的,这是相同的例外,当然我也修复了1和3尝试将数据源更改为数据源注意,它是大写的。初始目录是否也一样?将其更改为初始目录,并使用您可能遇到的其他类似属性。您介意发布整个配置文件吗?省略不相关的部分,例如应用程序键
An exception of type 'System.ArgumentException' occurred in EntityFramework.dll
keyword not supported: 'data source'.
public class FBuyShopContext : DbContext
{
public FbuyShopContext() : base("FBuyShopContext") {}
}
public class Product {
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}