【问题标题】:EF Code First .SaveChanges Updating Wrong TableEF Code First .SaveChanges 更新错误的表
【发布时间】:2011-03-14 10:38:56
【问题描述】:

我声明了下表

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。

【问题讨论】:

    标签: entity-framework asp.net-mvc-3 ef-code-first


    【解决方案1】:

    我认为[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 类,它实际上有一个 Name 属性。也许您使用了错误的命名空间。您需要 System.ComponentModel.DataAnnotations 命名空间中的 TableAttribute 类(在 EntityFramework.dll 程序集中)。

    【讨论】:

    • @Slauma 感谢您的回复。这是我的表声明的开始
    • @Slauma 对上次编辑感到抱歉。我误按了输入。无论如何感谢您的回复,但我仍然不知所措。我在 using 中声明了 ...DataAnnotations 和 ...Linq.Mapping。如果我删除这两个引用,Resolve 只会为我提供 ...Linq.Mapping for Table。 TableAttribute 似乎不是我的...DataAnnotations 的成员。您谈到 ...DataAnnotations 在我的 References 列表中的 EntityFramework.dll 中。我将如何添加对此版本的引用,而不是我在 .NETFramework 4 中包含的引用?
    • @xiecs:您是否使用实体框架 CTP5 进行代码优先开发?我的意思是从这个页面下载:microsoft.com/downloads/en/… 我不确定 TableAttribute 是否已经在 CTP4 中可用。但是我链接的下载中包含的EntityFramework.dll 肯定包含 ...DataAnnotations 命名空间中的 TableAttribute。我也会从你的引用中删除 System.Data.Linq 程序集(我认为那是 LINQ to SQL),除非你真的需要它用于项目中的其他目的。
    • @xiecs:我刚刚检查并比较了 CTP4 和 CTP5 的自述文件。事实上TableAttribute 在 CTP5 中是新的,在 CTP4 中不存在。只需检查您的版本,EntityFramework.dll 必须来自 2010 年 12 月。
    • @Slauma 我安装了 CTP5。我的 EntityFramework.dll 的日期为 2010 年 6 月 12 日。但是我的 ...DataAnnotations 命名空间仍然不包含 TableAttribute!我的运行时版本号为 4.0.30319。如果这是错误的,我显然需要以某种方式更新 APSP.NET;但我认为我是最新的,因为前几天我安装了 SP1。但是你认为这会覆盖 EF4 CTP5 安装的一些后来的模块吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多