EF Code First .SaveChanges 更新错误的表

发布于 2024-10-22 12:41:48 字数 2217 浏览 1 评论 0原文

我声明了下表,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-10-29 12:41:48

我认为 [Table( Name = "tblFixtures" )] 无效,因为 Name 不是 TableAttribute 类的属性 - 看起来像这样:

public class TableAttribute : Attribute
{
    public TableAttribute(string tableName);

    public string SchemaName { get; set; }
    public string TableName { get; }
}

所以,以下属性设置应该同时工作:

[Table("tblFixtures")]

[Table(TableName = "tblFixtures")]

编辑:但我现在想知道为什么你没有收到编译器错误?

我刚刚注意到 System.Data.Linq.Mapping 命名空间(在 System.Data.Linq.dll 程序集中)还有另一个 TableAttribute 类,它实际上有一个 <代码>名称属性。也许您使用了错误的命名空间。您需要来自 System.ComponentModel.DataAnnotations 命名空间(在 EntityFramework.dll 程序集中)的 TableAttribute 类。

I think that [Table( Name = "tblFixtures" )] is invalid because Name isn't a property of the TableAttribute class - which looks like this:

public class TableAttribute : Attribute
{
    public TableAttribute(string tableName);

    public string SchemaName { get; set; }
    public string TableName { get; }
}

So, the following attribute settings should work both:

[Table("tblFixtures")]

or

[Table(TableName = "tblFixtures")]

Edit: But I am wondering now why you didn't get a compiler error?

I just noticed that there is another TableAttribute class in the System.Data.Linq.Mapping namespace (in System.Data.Linq.dll assembly) which in fact has a Name property. Perhaps you have a wrong namespace-using. You need the TableAttribute class from the System.ComponentModel.DataAnnotations namespace (in EntityFramework.dll assembly).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文