2

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.

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

21 Comments

@Slauma Thanks for your reply. This is the start of my table declarations
@Slauma Sorry about the last edit. I hit enter by mistake. Thanks anyway for you reply, but I am still at a loss. I have both ...DataAnnotations and ...Linq.Mapping declared in using. If I delete both references, Resolve only offers me ...Linq.Mapping for Table. TableAttribute does not appear to be a member of my ...DataAnnotations. You talk about ...DataAnnotations being in EntityFramework.dll, which I have in my References list. How would I add a reference to this version rather than the one I have included from in .NETFramework 4?
@xiecs: Are you using Entity Framework CTP5 for Code-First development? I mean the download from this page: microsoft.com/downloads/en/… I am not sure if the TableAttribute was already available in CTP4. But the EntityFramework.dll which is included in the download I've linked contains definitely the TableAttribute in ...DataAnnotations namespace. I also would remove the System.Data.Linq assembly from your references (I think that's LINQ to SQL) unless you really need it for other purposes in your project.
@xiecs: I've just checked and compared the readme files for CTP4 and CTP5. And indeed TableAttribute is new in CTP5 and didn't exist in CTP4. Just check your version, EntityFramework.dll must be from December 2010.
@Slauma I have CTP5 installed. My EntityFramework.dll is dated 06/12/2010. But my ...DataAnnotations namespace still doesn't contain TableAttribute! Mine has a runtime version no of 4.0.30319. If that is wrong, I obviously need to update APSP.NET somehow; but I think I am on the latest as I have installed SP1 the other day. But could that have overwritten some later modules installed by EF4 CTP5 do you think?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.