Entity framework EF代码优先。保存更改更新错误的表

Entity framework EF代码优先。保存更改更新错误的表,entity-framework,asp.net-mvc-3,ef-code-first,Entity Framework,Asp.net Mvc 3,Ef Code First,我已申报下表 namespace RLSBCWebSite.Domain.Entities { [Table( Name = "tblFixtures" )] [DisplayColumn( "Date", "Date", false )] public class Fixture { [HiddenInput( DisplayValue = false )] [Column( IsPrimaryKey = true, IsDbG

我已申报下表

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" } );
        }
    }
}
当我尝试保存条目时,当执行.SaveChanges()语句时,我会得到一个SQL UpdateException,并显示以下错误消息

无效的对象名称“dbo.Fixtures”

为什么它要更新一个名为Fixtures的表?当然,它应该更新tblFixtures。

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

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

    public string SchemaName { get; set; }
    public string TableName { get; }
}
因此,以下属性设置应同时起作用:

[Table("tblFixtures")]

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


我刚刚注意到在
System.Data.Linq.Mapping
命名空间(在System.Data.Linq.dll程序集中)中还有另一个
TableAttribute
类,它实际上有一个
Name
属性。可能您使用的命名空间不正确。您需要
System.ComponentModel.DataAnnotations
命名空间(在EntityFramework.dll程序集中)中的
TableAttribute
类。

@Slauma感谢您的回复。这是我桌子的开始declarations@Slauma很抱歉上次编辑。我误按了回车键。无论如何谢谢你的回复,但我还是不知所措。我在using中声明了…DataAnnotations和…Linq.Mapping。若我同时删除这两个引用,Resolve只为我提供表的…Linq.Mapping。TableAttribute似乎不是my…DataAnnotations的成员。您谈到了EntityFramework.dll中的…DataAnnotations,我在引用列表中有它。如何添加对此版本的引用,而不是我在.NETFramework 4中包含的引用?@xiecs:您是否使用Entity Framework CTP5进行代码优先开发?我的意思是从这个页面下载:我不确定TableAttribute是否已经在CTP4中可用。但是我链接的下载中包含的
EntityFramework.dll
在…DataAnnotations命名空间中肯定包含TableAttribute。我还将从您的引用中删除
System.Data.Linq
程序集(我认为这是Linq到SQL),除非您确实需要它用于项目中的其他用途。@xiecs:我刚刚检查并比较了CTP4和CTP5的自述文件。实际上,
TableAttribute
在CTP5中是新的,在CTP4中不存在。只需检查您的版本,EntityFramework.dll必须从2010年12月开始。@Slauma我已经安装了CTP5。我的EntityFramework.dll日期为2010年12月6日。但是我的…DataAnnotations命名空间仍然不包含TableAttribute!Mine的运行时版本号为4.0.30319。如果这是错误的,我显然需要以某种方式更新APSP.NET;但我想我是最新的,因为我在前几天安装了SP1。但你认为这会覆盖EF4 CTP5安装的一些后续模块吗?
        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 );
    }
public class TableAttribute : Attribute
{
    public TableAttribute(string tableName);

    public string SchemaName { get; set; }
    public string TableName { get; }
}
[Table("tblFixtures")]
[Table(TableName = "tblFixtures")]