Can not insert row to database because of relationship problems c# entity-framework


First off, I'm using Entity Framework 5 and ASP.NET MVC 4. Additionally, this is my first MVC project.

My WorkLogs Table needs a row to be added. I started building my database from the code up.

This is my WorkLogs Model (I haven't changed it since I created the database):

public class WorkLogs
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int WorkLogId { get; set; }
    public int Time { get; set; }
    public DateTime Date { get; set; }
    public DateTime CreatedDate { get; set; }
    public bool IsSent { get; set; }

    public virtual Users User { get; set; }
    public virtual Works Work { get; set; }

This model generated the following table in the database:


And here are the relationships (I believe this to be accurate):


With the following code in my controller, I'm attempting to add a new row:

WorkLogs log = new WorkLogs()
    Time = iTime,
    Date = beginWeek.AddDays(i),
    CreatedDate = DateTime.Now,
    User = userObj, 
    Work = workObj, 
    IsSent = true


When I run this, the line _db.SaveChanges(); throws an exception.

An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.

When I observe this exception, I realize there is a problem with relationships. Additionally, I am aware that I am attempting to put an object into a column that requests an integer. I'm not sure why it created the User UserId and Work WorkId columns or why it assigned them an int data type, though. But a user object is desired in the controller.

My controller and model could be completely off. However, I'm new to EF and MVC as I mentioned.

What adjustments must I make in order to add a WorkLog to my database?

I believe I was clear.

We would appreciate any assistance.

8/7/2014 7:37:18 AM

Accepted Answer

A is the internal exception.SqlException . 'An out-of-range value was produced by the conversion of a datetime2 data type to a datetime data type.' You'll quickly discover why if you look for that message:

Your goal isCreatedDate and Date on WorkLogs to reasonable values, the offender is probablyCreatedDate and/or UpdatedDate in the event thatWorks .

If you haven't changed them,.NET will use their default values.DateTime.MinValue - year 0001. You will have an overflow if you attempt to insert it into the database since in SQL Server the minimal value isSqlDateTime.MinValue - year 1753.

I advise you to alter the value ofCreatedDate to DateTime.Now the type of in your constructor andUpdatedDate a nullableDateTime (The default for them will be null instead ofDateTime.Min )

5/23/2017 12:17:05 PM

Popular Answer


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow