EF 4.1 code first having issues in inserting parent and child objects
I am using Ef 4.1 code first.
I am getting the follwing error and not sure what I am doing wrong:
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.
Inner exception:
Invalid column name 'GrantApplicationIs'. Invalid column name 'GrantApplication_Id'.
I have a grant application and I am trying to add an audit entry to it when I save it to the database.
Here is my context:
public class HbfContext : DbContext
{
public DbSet<Bank> Banks { get; set; }
public DbSet<AccountType> AccountTypes { get; set; }
public DbSet<GrantApplication> GrantApplications { get; set; }
public DbSet<AuditEntry> AuditEntries { get; set; }
}
Grant Application class:
public class GrantApplication
{
public int Id { get; set; }
public string EmployeeNumber { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<AuditEntry> AuditEntries { get; set; }
}
AuditEntry class:
public class AuditEntry
{
public int Id { get; set; }
public int OldValue { get; set; }
public int NewValue { get; set; }
public DateTime AuditDate { get; set; }
public string EmployeeNumber { get; set; }
public int GrantApplicationIs { get; set; }
public GrantApplication GrantApplication { get; set; }
}
This is how I add a new audit entry to a new grant application:
public void Insert(GrantApplication grantApplication)
{
DateTime currentDateTime = DateTime.Now;
string submitterEmployeeNumber = "123456";
grantApplication.SignatureDate = currentDateTime;
grantApplication.SubmitterEmployeeNumber = submitterEmployeeNumber;
// Add audit entry
grantApplication.AuditEntries = new List<AuditEntry>();
grantApplication.AuditEntries.Add(new AuditEntry
{
NewValue = grantApplication.GrantApplicationStateId,
AuditDate = currentDateTime,
EmployeeNumber = submitterEmployeeNumber
});
// Insert the new grant application
grantApplicationRepository.Insert(grantApplication);
}
UPDATE:
My table structures looks like this:
GrantApplications
table:
Id int
EmployeeNumber varchar(6)
Title varchar(10)
FirstName varchar(50)
LastName varchar(50)
AuditEntries
table:
Id int
GrantApplicationId int
OldValue int
NewValue int
AuditDate datetime
EmployeeNumber varchar(6)
I have no idea what GrantApplicationIs
and GrantApplication_Id
and why they are column names. From what?
For me it looks that this is the source of the problem:
public int GrantApplicationIs { get; set; }
Is this a typo? Shoudn't it be GrantApplicationId
? Anyway the effect is:
GrantApplicationIs
isn't recognized as the foreign key property for theGrantApplication
navigation property. Instead it's an ordinary scalar property. If you don't have a column named like this in the DB you get your first exception: Invalid column name 'GrantApplicationIs'.EF uses the default name for the FK database column which is
GrantApplication_Id
. If you don't have a column named like this in the DB you get your second exception: Invalid column name 'GrantApplication_Id'.
Solution:
- Either name the FK property according to conventions (
GrantApplicationId
) - Or apply data annotations or Fluent API to tell EF that
GrantApplicationIs
is indeed your FK property (and create an appropriate column in the DB).
精彩评论