Answer the question
In order to leave comments, you need to log in
How to make a one-to-one relationship Entity, CodeFirst, C#?
Guys, please tell me how to make a one-to-one connection in Code First. Entity 5.0
I have a table "Cards", with fields: Card Number and Record Id.
And there is a "Records" table where there is an Id of the Record and a few more purely informative fields, date, time ...
So, when a card is issued to the "Cards" table, the Card Number is written, let's say "1" and Id of the Record, let's say "21". Accordingly, IdRecords (21) and other informative fields are written to the "Records" table. When the card is returned, we find the number of the returned card (1) in the CardNumber field and delete this entry, while everything remains in the "Records" table.
Those. the "Cards" table should be dynamic, I wanted to do it without the Id field,
What is the best way to organize fields in a table? As I understand it, the relationship is created one to one, but will I have any problems when deleting from the "Cards" table? How to make a unique field "CardNumber"? those. so that the numbers do not repeat there. Is the id field required? How can you do without it? I would really appreciate an example of this class!
Answer the question
In order to leave comments, you need to log in
public class Card
{
public int CardId { set; get; }
public Record Record { set; get; }
}
public class Record
{
public int RecordId { set; get; }
}
modelBuilder.Entity<Card>()
.HasRequired(x => x.Record)
.WithRequiredPrincipal()
.WillCascadeOnDelete(false);
In 95% of cases, when you need a one-to-one relationship, you have not designed the database correctly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question