I wrote a class Patient
which is supposed to be an object representation of my relation. Then I wrote a context class which inherits from DbContext
and which contains a public property DbSet
of Patients
. All right. Now, how do I say to Entity that this DbSet
should work with TBL_PATIENTS
table in the database? Where do I configure that?
(well, as you can see i'm very new to .NET and Entity, so any help is appreciated)
You need to create an EntityTypeConfiguration<Patient>
and initialise it in your Context
class.
eg
Context
:
public class MyContext : DbContext
{
static MyContext()
{
Database.SetInitializer<MyContext>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new PatientMap());
}
}
Map
:
public class PatientMap: EntityTypeConfiguration<Patient>
{
public PatientMap()
{
//table
this.ToTable("TBL_PATIENTS", "dbo");
//PK
this.HasKey(t => t.id);
//Columns
this.Property(t => t.forenames)
.HasColumnName("FIRSTNAMES")
.IsRequired()
.HasMaxLength(50);
// Relationships
this.HasRequired(t => t.Ward)
.WithMany(t => t.Patients)
.HasForeignKey(d => d.Ward_id);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.