Home > Net >  Can in EntityFrameWork, dynamic field mapping for entities and tables?
Can in EntityFrameWork, dynamic field mapping for entities and tables?

Time:09-25

[size=12 px] defines an entity class, for example,
The class the UserInfo
{
[DatabaseGenerated (DatabaseGeneratedOption. Identity)]//specify this column is on the column of
Public decimal ID {get; set; }

Public string UserName {get; set; }

Public string Password {get; set; }
}

===============
In the DbContext OnModelCreating, usually need a class of a class to specify configuration, such as:

Protected override void OnModelCreating (DbModelBuilder modelBuilder)
{
JAPIBASE modelBuilder. HasDefaultSchema (" ");
ModelBuilder. Entity (a). ToTable (" the USERINFO ");

//the increasing sequence
ModelBuilder. Entity (a). HasKey (o=& gt; O.I D);

ModelBuilder. Entity (a). The Property (o=& gt; O.U serName). HasColumnName (" USERNAME ");

ModelBuilder. Entity (a). The Property (o=& gt; O.P assword). HasColumnName (" PASSWORD ");
}

=======================
My goal was to can pass a generic generic function, traverse the entity and then automatically, automatic mapping attributes to the table, on the field of line not line?
As follows: but in the property () here, what should be brought into the parameter error, not achieve them, anyone give?

///& lt; Summary>
///use generic methods
///(applicable to undefined on the keyword or keyword for ID on the entities and table mapping)
///& lt;/summary>
///& lt; Typeparam name="T" & gt;
///& lt; Param name="modelBuilder & gt;"
///& lt; Param name="pojo" & gt;
Public static void MapToTable (ref DbModelBuilder modelBuilder,
Bool hasIncreaseIDKey=false)
Where T: the class//where... Can only be carried out to illustrate the generic type T for type conversion, otherwise cannot be used T in the generic method parameter.
{

String propertyName;

ModelBuilder. Entity (). ToTable (typeof (T). The Name. The ToUpper ());



Foreach (PropertyInfo PI in typeof (T). The GetProperties ())//traverse the class members, and get the names of all the attribute
{
PropertyName=PI. The Name;

//the increasing sequence
If (hasIncreaseIDKey==true & amp; & PropertyName. The ToUpper ()=="ID")
ModelBuilder. Entity (a). HasKey (o=& gt; O.G etType (.) GetProperty (PI) Name));

If (propertyName! {
="")
ModelBuilder. Entity (a). The Property ( o=& gt; O.G etType (.) GetProperty (PI) Name) ) HasColumnName (propertyName. The ToUpper ());
}

}

}
Boldface places don't know what to pass parameters to the

CodePudding user response:

Well,
1, first of all, I personally don't like fluent way of setting,
I prefer to use annotations/feature

2 I don't remember need each field mapping,
Since you just case is different, you just need to set modelBuilder ignore case,

3 I've never used dynamic reflection this to do,
Because I have no more than two step operation,,, so don't bother,

CodePudding user response:

I'm EF Oracle database, Oracle are all uppercase, annotation line is in form, but in codefirst mode, handwritten class and table names are not consistent, so I feel is more fluent form some clear focus, but this on the field in Oracle seemed clear with him, it will have to turn the entity to get, escape not to drop, as for the case to ignore, I go to! I really don't know, I go to!
  •  Tags:  
  • C#
  • Related