Can not insert row to database because of relationship problems

asp.net-mvc asp.net-mvc-4 c# entity-framework

Question

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; }
    [Required]
    public int Time { get; set; }
    [Required]
    public DateTime Date { get; set; }
    [Required]
    public DateTime CreatedDate { get; set; }
    [Required]
    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:

db

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

db

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
};

_db.WorkLogs.Add(log);
_db.SaveChanges();

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.

1
2
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 )

7
5/23/2017 12:17:05 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

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