2011-03-14 42 views
2

我有以下表聲明EF代碼優先.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'。

爲什麼它試圖更新一個名爲夾具的表?當然,它應該更新tblFixtures。

回答

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程序集中)。

+0

@Slauma謝謝你的回覆。這是我的表格聲明的開始 – xiecs 2011-03-15 11:30:33

+0

@Slauma對上次編輯抱歉。我打錯了。非常感謝您的回覆,但我仍然處於虧損狀態。我有兩個... DataAnnotations和... Linq.Mapping聲明**使用**。如果我刪除這兩個引用,Resolve只提供給我... Linq.Mapping for Table。 TableAttribute似乎不是我的... DataAnnotations的成員。你談論... DataAnnotations在EntityFramework.dll中,我在**參考**列表中。我如何添加對這個版本的引用,而不是從.NETFramework 4中包含的引用? – xiecs 2011-03-15 12:20:29

+0

@xiecs:您是否使用實體框架CTP5進行Code-First開發?我的意思是從此頁下載:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=35adb688-f8a7-4d28-86b1-b6235385389d我不確定TableAttribute是否已經在CTP4中可用。但是,包含在我鏈接的下載中的'EntityFramework.dll'確實包含了... DataAnnotations命名空間中的TableAttribute。我也會從你的引用中刪除'System.Data.Linq'程序集(我認爲這是LINQ to SQL),除非你真的需要它在你的項目中用於其他目的。 – Slauma 2011-03-15 12:56:11