none
EF5 RC Code First - why no foreign key? why the datetime2 out-of-range value error? RRS feed

  • Question

  • I have the following simple model and it behaves not as I expect. It is a simple model to learn about EF and for this exercise I need some fields in every table so I created a 'BaseModel' class to contain the fields.

        public class Blog : BaseModel
        {
            [Key]
            public int BlogId { get; set; }
            public string Title { get; set; }
            public string BloggerName { get; set; }
            public virtual ICollection<Post> Posts { get; set; }
        }
    
        public class Post : BaseModel
        {
            [Key]
            public int PostId { get; set; }
            public string Title { get; set; }
            public DateTime DateCreated { get; set; }
            public string Content { get; set; }
            public int BlogId { get; set; }
        }
    
        public class BaseModel
        {
            public int LanguageId { get; set; }
            public DateTime CreationDate { get; set; }
            public string CreatedBy { get; set; }
        }
    
        public class Language
        {
            [Key]
            public int LanguageId { get; set; }
            public string Title { get; set; }
        }
    

    If I run this to create the database, it correctly creates 3 tables with the basemodel fields in there. However, no foreign key to the Language table has been created. So the LanguageId is there, but no foreign key.

    Besides this issue, I also run into the famous "The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated."

    This error only occurs if the 'DateTime' field in the 'BaseModel' is there. If I remove that field the error is gone. If there would be an issue with DateTime fields, then I would expect this also to occur with the DateTime field in 'Post' class, but this is not the case.

    Can anyone help me with these two issues?

    Thanks in advance!


    Didago IT Consultancy "Connecting People and Systems Using Microsoft Technology"

    Thursday, July 12, 2012 9:33 AM

Answers

  • Hi,

    first of all, you should change this

    public int BlogId { get; set; }

    to this

    public virtual Blog Blog { get; set; }

    I'm not sure about your firs issue. In fact, it should create the foreign key in the database; I think it's worth rechecking it in the table;

    About the second issue:

    When you say this:

    public DateTime CreationDate { get; set; }

    it's going to create a non-nullable datetime field in the database, that will expect values from this range - January 1, 1753 - December 31, 9999

    when you're trying to save the Post entity without setting it's CreationDate property manually, it'll have the C# minimum value for DateTime before going to the databese; the minimum value for DateTime in .NET is January 1, 0001 and the CreationDate field in the Posts table won't allow it (as minimum for it is January 1, 1753)

    Hope you'll find this helpful.

    Regards,

    Giorgi


    Clarity VS Precision

    Thursday, July 12, 2012 3:10 PM
  • Hi Jean-Paul Smit,

    Welcome to MSDN Forum.

    Please refer to the code below, this can correctly help you to create the foreign key. In your initial code, 'Blog' and 'Post' all contain a 'LanguageID' column, but it doesn't considered as a FK, becuase there's no any relationship between 'Blog' and 'Language' or 'Post' and 'Language' in the models.

    public class Blog : BaseModel
        {
            [Key]
            public int BlogId { get; set; }
            public string Title { get; set; }
            public string BloggerName { get; set; }
            public virtual ICollection<Post> Posts { get; set; }
        }
    
        public class Post : BaseModel
        {
            [Key]
            public int PostId { get; set; }
            public string Title { get; set; }
            public DateTime DateCreated { get; set; }
            public string Content { get; set; }
            public int BlogId { get; set; }
        }
    
        public class BaseModel
        {
            
            public DateTime CreationDate { get; set; }
            public string CreatedBy { get; set; }
            public virtual Language lan { get; set; }
        }
    
        public class Language
        {
            [Key]
            public int LanguageId { get; set; }
            public string Title { get; set; }
        }    

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 16, 2012 2:45 AM
    Moderator

All replies

  • Hi,

    first of all, you should change this

    public int BlogId { get; set; }

    to this

    public virtual Blog Blog { get; set; }

    I'm not sure about your firs issue. In fact, it should create the foreign key in the database; I think it's worth rechecking it in the table;

    About the second issue:

    When you say this:

    public DateTime CreationDate { get; set; }

    it's going to create a non-nullable datetime field in the database, that will expect values from this range - January 1, 1753 - December 31, 9999

    when you're trying to save the Post entity without setting it's CreationDate property manually, it'll have the C# minimum value for DateTime before going to the databese; the minimum value for DateTime in .NET is January 1, 0001 and the CreationDate field in the Posts table won't allow it (as minimum for it is January 1, 1753)

    Hope you'll find this helpful.

    Regards,

    Giorgi


    Clarity VS Precision

    Thursday, July 12, 2012 3:10 PM
  • Thanks for your reply!

    As far as I know both options (int BlogId and virtual Blog) are possible as of conventions.

    The other I would understand if both DateTime fields would fail, but that is not the case. Only the second fails but the " public DateTime DateCreated { get; set; }" of the Blog class is created correctly.

    I think I'll stick with database first, because this code first gives me more headache then I expected.


    Didago IT Consultancy "Connecting People and Systems Using Microsoft Technology"

    Thursday, July 12, 2012 7:54 PM
  • Hi Jean-Paul Smit,

    Welcome to MSDN Forum.

    Please refer to the code below, this can correctly help you to create the foreign key. In your initial code, 'Blog' and 'Post' all contain a 'LanguageID' column, but it doesn't considered as a FK, becuase there's no any relationship between 'Blog' and 'Language' or 'Post' and 'Language' in the models.

    public class Blog : BaseModel
        {
            [Key]
            public int BlogId { get; set; }
            public string Title { get; set; }
            public string BloggerName { get; set; }
            public virtual ICollection<Post> Posts { get; set; }
        }
    
        public class Post : BaseModel
        {
            [Key]
            public int PostId { get; set; }
            public string Title { get; set; }
            public DateTime DateCreated { get; set; }
            public string Content { get; set; }
            public int BlogId { get; set; }
        }
    
        public class BaseModel
        {
            
            public DateTime CreationDate { get; set; }
            public string CreatedBy { get; set; }
            public virtual Language lan { get; set; }
        }
    
        public class Language
        {
            [Key]
            public int LanguageId { get; set; }
            public string Title { get; set; }
        }    

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 16, 2012 2:45 AM
    Moderator