设定连接字符串时,Name属性的值,应该等于创建的EF数据库类类名
比如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using MVC_example.Models;
namespace MVC_example.DataAccessLayer
{
public class Cnn : DbContext
{
private DbSet<User> _users;
public DbSet<User> Users
{
get
{
return _users;
}
set
{
_users = value;
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().ToTable("tUser");
base.OnModelCreating(modelBuilder);
}
}
}
其中定义了类名为Cnn,则Web.config中的ConnectionString
应为:
<connectionStrings>
<add connectionString="Data Source=.;Initial Catalog=SalesERPDB;Integrated Security=True;" name="Cnn" providerName="System.Data.SqlClient" />
</connectionStrings>