奇怪的问题我在表上有一个主键,但是当我编译并运行我的程序实体时,制动并抱怨它不存在。
这是我得到的错误:
在模型生成期间检测到一个或多个验证错误:
WindowsFormsApplication1.Coverage:EntityType' Coverage'没有定义键。定义此EntityType的键 覆盖范围:EntityType:EntitySet' Coverages'是基于类型'覆盖范围'没有定义键
即使它在我的SQL Server数据库中被定义为主键并且是标识列。
现在我通过Code切换到我的数据库,我想知道这是我的问题的一部分。
public string SetupConnections()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
try
{
// Specify the provider name, server and database.
string providerName = "System.Data.SqlClient";
string serverName = "sourcenet";
string databaseName = "COASTALCAROLINAPODIATRY";
// Initialize the connection string builder for the underlying provider.
// Set the properties for the data source.
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = true;
// sqlBuilder.UserID = "scheduler";
// sqlBuilder.Password = "Y=1.5x+10";
// Build the connection string.
string providerString = sqlBuilder.ToString();
// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
// Set the provider name.
entityBuilder.Provider = providerName;
// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;
// Set the Metadata location.
entityBuilder.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";
Console.WriteLine(entityBuilder.ToString());
using (EntityConnection conn = new EntityConnection(entityBuilder.ToString()))
{
conn.Open();
Console.WriteLine("Just testing the connection.");
conn.Close();
}
}
然后在我的模型中我有以下
public SMBASchedulerEntities() : base("name=SMBASchedulerEntities")
{
}
public SMBASchedulerEntities(string connectionstring) : base(connectionstring)
{
}
public virtual DbSet<Appointment> Appointments { get; set; }
public virtual DbSet<AppointmentType> AppointmentTypes { get; set; }
public virtual DbSet<Carrier> Carriers { get; set; }
public virtual DbSet<NextID> NextIDs { get; set; }
public virtual DbSet<Option> Options { get; set; }
public virtual DbSet<Patient> Patients { get; set; }
public virtual DbSet<Policy> Policies { get; set; }
public virtual DbSet<Provider> Providers { get; set; }
public virtual DbSet<Referral> Referrals { get; set; }
public virtual DbSet<Coverage> Coverages { get; set; }
修改2
好的,所以作为测试,我删除了连接字符串对象并将其放在这里,而不是像这样,并且想知道出了什么问题。
这可以传递连接字符串吗?
public SMBASchedulerEntities(string connectionstring) : base("name=SMBASchedulerEntities")
{
}
编辑3
这是连接构建器创建的内容:
Data Source=sourcenet;Initial Catalog=COASTALCAROLINAPODIATRY;Integrated Security=True
代码:
public SMBASchedulerEntities(string connectionstring) : base(connectionstring)
{
}
当传递给上述方法时哪个不起作用我相信那里有人已经这样做了。