EF Code First .SaveChanges 更新错误的表
我声明了下表,
namespace RLSBCWebSite.Domain.Entities
{
[Table( Name = "tblFixtures" )]
[DisplayColumn( "Date", "Date", false )]
public class Fixture
{
[HiddenInput( DisplayValue = false )]
[Column( IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert )]
public int FixtureID { get; set; }
[Required( ErrorMessage = "Please select Male, Female or Mixed" )]
public string Gender { get; set; }
...... more columns
public IEnumerable<ValidationResult> Validate( ValidationContext validationContext )
{
if (((ScoreFor > 0) || (ScoreAgainst > 0)) && (!String.IsNullOrEmpty( Comments )))
yield return new ValidationResult( "Please complete either ScoreFor & ScoreAgainst or Comments!", new[] { "Comments" } );
}
}
}
我的 DbContext 如下:
namespace RLSBCWebSite.Domain.Entities
{
public class RLSBCWebSiteDb : DbContext
{
public DbSet<Officer> tblOfficers { get; set; }
public DbSet<Fixture> tblFixtures { get; set; }
public DbSet<CompetitionWinner> tblCompetitionWinners { get; set; }
}
}
我的 Web.Config 具有:
<connectionStrings>
<add name="RLSBCWebSiteDb"
connectionString="data source=MAINPC\SQLEXPRESS;Initial Catalog=RLSBCWebSite;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
我的控制器代码是:
RLSBCWebSiteDb rlsbcWebSite = new RLSBCWebSiteDb();
[HttpPost]
public ActionResult CreateFixture(Fixture fixture)
{
if (ModelState.IsValid)
{
rlsbcWebSite.tblFixtures.Add( fixture );
rlsbcWebSite.SaveChanges();
return RedirectToAction( "MensResults", new { id = fixture.MatchDate.Year.ToString() } );
}
return View( fixture );
}
当我尝试保存条目时,执行 .SaveChanges() 语句时收到 SQL UpdateException 和以下错误消息。
对象名称“dbo.Fixtures”无效。
为什么它要尝试更新名为 Fixtures 的表?当然,它应该更新 tblFixtures。
I have the following table declared
namespace RLSBCWebSite.Domain.Entities
{
[Table( Name = "tblFixtures" )]
[DisplayColumn( "Date", "Date", false )]
public class Fixture
{
[HiddenInput( DisplayValue = false )]
[Column( IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert )]
public int FixtureID { get; set; }
[Required( ErrorMessage = "Please select Male, Female or Mixed" )]
public string Gender { get; set; }
...... more columns
public IEnumerable<ValidationResult> Validate( ValidationContext validationContext )
{
if (((ScoreFor > 0) || (ScoreAgainst > 0)) && (!String.IsNullOrEmpty( Comments )))
yield return new ValidationResult( "Please complete either ScoreFor & ScoreAgainst or Comments!", new[] { "Comments" } );
}
}
}
My DbContext is as follows:
namespace RLSBCWebSite.Domain.Entities
{
public class RLSBCWebSiteDb : DbContext
{
public DbSet<Officer> tblOfficers { get; set; }
public DbSet<Fixture> tblFixtures { get; set; }
public DbSet<CompetitionWinner> tblCompetitionWinners { get; set; }
}
}
My Web.Config has:
<connectionStrings>
<add name="RLSBCWebSiteDb"
connectionString="data source=MAINPC\SQLEXPRESS;Initial Catalog=RLSBCWebSite;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
My contoller code is:
RLSBCWebSiteDb rlsbcWebSite = new RLSBCWebSiteDb();
[HttpPost]
public ActionResult CreateFixture(Fixture fixture)
{
if (ModelState.IsValid)
{
rlsbcWebSite.tblFixtures.Add( fixture );
rlsbcWebSite.SaveChanges();
return RedirectToAction( "MensResults", new { id = fixture.MatchDate.Year.ToString() } );
}
return View( fixture );
}
When I try and save the entry, I get an SQL UpdateException with the following error message when the .SaveChanges() statement is executed.
Invalid object name 'dbo.Fixtures'.
Why is it trying to update a table called Fixtures? Surely, it should be updating tblFixtures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
[Table( Name = "tblFixtures" )]
无效,因为Name
不是TableAttribute
类的属性 - 看起来像这样:所以,以下属性设置应该同时工作:
或
编辑:但我现在想知道为什么你没有收到编译器错误?
我刚刚注意到
System.Data.Linq.Mapping
命名空间(在 System.Data.Linq.dll 程序集中)还有另一个TableAttribute
类,它实际上有一个 <代码>名称属性。也许您使用了错误的命名空间。您需要来自System.ComponentModel.DataAnnotations
命名空间(在 EntityFramework.dll 程序集中)的TableAttribute
类。I think that
[Table( Name = "tblFixtures" )]
is invalid becauseName
isn't a property of theTableAttribute
class - which looks like this:So, the following attribute settings should work both:
or
Edit: But I am wondering now why you didn't get a compiler error?
I just noticed that there is another
TableAttribute
class in theSystem.Data.Linq.Mapping
namespace (in System.Data.Linq.dll assembly) which in fact has aName
property. Perhaps you have a wrong namespace-using. You need theTableAttribute
class from theSystem.ComponentModel.DataAnnotations
namespace (in EntityFramework.dll assembly).