问题描述
好吧,这可能不与EF。我试图用code-第一特征,下面是我写的: -
Ok, this may not be related to EF. I am trying to use the code-first feature and following is what I wrote:-
var modelBuilder = new ModelBuilder();
var model = modelBuilder.CreateModel();
using (AddressBook context = new AddressBook(model))
{
var contact = new Contact
{
ContactID = 10000,
FirstName = "Brian",
LastName = "Lara",
ModifiedDate = DateTime.Now,
AddDate = DateTime.Now,
Title = "Mr."
};
context.contacts.Add(contact);
int result = context.SaveChanges();
Console.WriteLine("Result :- "+ result.ToString());
}
上下文类: -
The context class:-
public class AddressBook : DbContext
{
public AddressBook()
{ }
public AddressBook(DbModel AddressBook)
: base(AddressBook)
{
}
public DbSet<Contact> contacts { get; set; }
public DbSet<Address> Addresses { get; set; }
}
和连接字符串: -
and the connection string:-
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="AddressBook" connectionString="Data Source=MyMachine;Initial Catalog=AddressBook;Integrated Security=True;MultipleActiveResultSets=True;providerName=System.Data.SqlClient"/>
</connectionStrings>
</configuration>
所以,数据库名称为通讯录,当我初始化地址簿对象的错误发生。我看到有人建议增加(的providerName =System.Data.SqlClient的)的连接字符串。不过,这并不在我的情况下工作。我缺少什么吗?
So, the database name is "AddressBook" and the error happens when I initialize the AddressBook object. I see people suggesting to add (providerName="System.Data.SqlClient") to the connection string. But It does not work in my case. Am I missing anything here?
推荐答案
您应该添加System.Data.SqlClient的作为值到一个名为的ProviderName的新属性。就像如下: -
You should add the System.Data.SqlClient as the value to a new attribute named "ProviderName". Just like below :-
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="AddressBook" providerName="System.Data.SqlClient" connectionString="Data Source=MyMachine;Initial Catalog=AddressBook;Integrated Security=True;MultipleActiveResultSets=True;"/>
</connectionStrings>
</configuration>
这篇关于实体框架 - 为“providerInvariantName'参数期待非空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!