none
Model State Error. Entity has no key. RRS feed

  • Question

  • I see lots of posts on this subject. I create the tables in the database to simulated database first. When I create a table with an identity column and autoincrement set. EDF has no problem with finding the key. But if create the table with any other data type it chokes.

    here is the class definition for State: (notice last using statment)

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Linq.Mapping;
    
    
    namespace SportsStore.Domain.Entities
    {
     [Table(Name = "States")]
     public class State
     {
     [Column(IsPrimaryKey=true, IsDbGenerated=false)]
     public string StateCode { get; set; }
    
     [Column]
     public string StateName { get; set; }
     }
    }
    

    Now look at a class definition that EDF has no problems with:

     

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Linq.Mapping;
    
    namespace SportsStore.Domain.Entities
    {
     [Table(Name = "Users")]
     public class User
     {
     [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
     public int UserId { get; set; }
    
     [Column]
     public string UserEmail { get; set; }
    
     [Column]
     public string UserName { get; set; }
    
     [Column]
     public string UserPassword { get; set; }
    
     }
    }
    

    If I add a column to the States table in the database, change its type to int and name it StateCodeNo I will still get the error. If I rename it to StateID then the application runs without error. Of course in both case I also added the Property Name with the same name as the new column name. So the following causes no error: (aslo the datatype does not seem to matter  - what matter is the column name and property name have "ID" in its name.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Linq.Mapping;
    
    
    namespace SportsStore.Domain.Entities
    {
     [Table(Name = "States")]
     public class State
     {
     [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert )]
     public string StateID { get; set; }
    
     [Column]
     public string StateCode { get; set; }
    
     [Column]
     public string StateName { get; set; }
     }
    }

    Any help on this is appreciated. Also using [Key] annotation and adding a using statement for System.ComponentModel.DataAnnotations does work. That conflicts with System.Data.Linq.Mapping.
    WhiskeyRomeoLima

    Friday, August 12, 2011 6:46 PM

Answers

  • It appears I need to something like this (being new to EF I don't understand Linq Mapping versus Component Model Data Annotations -- all I know is this solved my problem)

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Linq.Mapping;
    using System.ComponentModel.DataAnnotations;
    
    namespace SportsStore.Domain.Entities
    {
      [System.Data.Linq.Mapping.Table(Name = "Countries")]
      public class Country
      {
        [System.Data.Linq.Mapping.Column(IsPrimaryKey = true)]
        [Key]
        public string CountryCode { get; set; }
    
        [System.Data.Linq.Mapping.Column]
        public string CountryName { get; set; }
      }
    }


    WhiskeyRomeoLima
    Sunday, August 14, 2011 5:38 PM

All replies

  • It appears I need to something like this (being new to EF I don't understand Linq Mapping versus Component Model Data Annotations -- all I know is this solved my problem)

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Linq.Mapping;
    using System.ComponentModel.DataAnnotations;
    
    namespace SportsStore.Domain.Entities
    {
      [System.Data.Linq.Mapping.Table(Name = "Countries")]
      public class Country
      {
        [System.Data.Linq.Mapping.Column(IsPrimaryKey = true)]
        [Key]
        public string CountryCode { get; set; }
    
        [System.Data.Linq.Mapping.Column]
        public string CountryName { get; set; }
      }
    }


    WhiskeyRomeoLima
    Sunday, August 14, 2011 5:38 PM
  • Hi WhiskeyRomeoLima,

    Welcome!

    There are two ways configuring our modules: Data Annotations and Fluent API.

    For example: 

    ColumnAttribute
    Placed on a property to specify the column name, ordinal & data type KeyAttribute Primary key.

    (http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx)

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 15, 2011 6:08 AM
    Moderator
  • Hi Allen,

    Thanks for the reply.  I am not doing code first because in my world that never happens.  But from your link I got to a comment that provided this link which answered part of my question.

    http://forums.asp.net/p/1692975/4473300.aspx/1?Code+First+how+to+create+simple+relationship

    But it does not explain why these overlapping namespacse exists:  System.Data.Linq.Mapping versus this namespace - System.ComponentModel.DataAnnotations.  Also it appears that System.Data.Linq.Mapping will not work at all unless the Primary key is named Id or ClassNameId.  There must be some intention to use one or the other.

    Again, thanks for the reply.

    Bill

     


    WhiskeyRomeoLima
    Monday, August 15, 2011 3:39 PM
  • Hi Bill,

    System.Data.Linq.Mapping NameSpace is used to generate a LINQ to SQL object model that represents the structure and content of a relational database.

    System.componentModel.DataAnnotations NameSpace  Provides attribute classes that are used to define metadata for entity classes.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 25, 2011 7:06 AM
    Moderator
  • Alen,

    Thanks for  the reply and the clarification.  Why do these classes intefer?  Are they to used mutually exclusive of one another?  And whcy is System.Data.Ling.Mapping NamsSpace limited to only recongnizing PK that are named "ID" or named liks so entityNameID?

    I really appreciated the clarfication.

    Bill


    WhiskeyRomeoLima
    Thursday, August 25, 2011 2:35 PM