1

I am using Entity Framework Core in my project. My entity looks like this:

[Table("Policy")]
public class Policy
{
    [Key]
    public Guid Id { get; set; }    
    public Bank Bank { get; set; }          
    public string LastUpdateBy { get; set; }    
}

[Table("Bank")]
public class Bank
{
    [Key]
    public Guid Id { get; set; }    
    public string Code { get; set; }    
    public DateTime? CreateTs { get; set; } 
}

In SQL, this is how table Policy looks like:

Id (uniqueidentifier)
Bank (varchar)
LastUpdateBy (varchar)

I am referencing Bank in Policy entity because i would like to have a parent-child relationship.

When I am trying to add a record in Policy table, it's giving me following error

Violation of PRIMARY KEY constraint 'PK_Bank'. Cannot insert duplicate key in object 'dbo.Bank'. The duplicate key value is (829e0798-c7ee-4fcf-b18d-0fbc60f63fad).

Here is my code:

var policy = new Policy
{
    Bank = new Bank
    {
        Id = "829e0798-c7ee-4fcf-b18d-0fbc60f63fad"
    },
    Id = Guid.NewGuid(),
    LastUpdateBy = "User"
};

dbContext.Policy.Add(policy);
dbContext.SaveChanges();

I know, it's trying to add a record in Bank table as well. How would I tell EF Core to insert record only in Policy table?

4
  • 4
    Hold a foreign key of the Bank's GUID inside of class Policy, not a Bank object Commented Aug 9, 2019 at 16:59
  • Any other way without changing entity? Commented Aug 9, 2019 at 17:01
  • Not that I am aware of, besides why would you not want to structure your entities correctly? A policy in no way should be creating a bank, that makes no sense. Commented Aug 9, 2019 at 17:02
  • 3
    The other ways would be to attach the bank instance to the dbcontext so it is tracked and marked as not changed. Then it won't be inserted on save changes. It would make more sense to include the fk in the child entity (Policy) and just set that instead of creating a new bank instance. Commented Aug 9, 2019 at 17:17

2 Answers 2

2

A bank is an entity that exists as stated a (singleton), a bank will not change and it's properties are it's own. A policy is issued by a bank, therefore, a policy should reference a bank via the banks Guid or ID property (foreign key), this will then create the parent -> child relationship which you desire. This is a ONE-TO-MANY relationship. I think you may want to either spend some more time learning about database relational design or draw these things out on paper before creating your entity classes.

EDIT: Igor has given you an option to stay the course and not change your entities, but I think you should take a good hard look at structuring your entities in a way that would mirror a database relational design. Just my opinion.

Sign up to request clarification or add additional context in comments.

Comments

2

If you don't want to introduce a foreign key for Bank then you have to attach bank first.

var bank = new Bank
{
    Id = "829e0798-c7ee-4fcf-b18d-0fbc60f63fad"
};
dbContext.Bank.Attach(bank);

var policy = new Policy
{
    Bank = bank,
    Id = Guid.NewGuid(),
    LastUpdateBy = "User"
};

dbContext.Policy.Add(policy);
dbContext.SaveChanges();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.