Home > Net >  C # ef ToTable (tablename, schame), dynamic loading schame is invalid
C # ef ToTable (tablename, schame), dynamic loading schame is invalid

Time:03-19

Doing an asp.net core project, because with plug-in development,
Then, the dynamic mapping to the table,
If all the tables in the same database ihs, then this code is no problem,
But I want to put the parts (assets), and then add a schame parameters, but found that don't work, he was mapped to ihs. XXXX,
Before I make a project, not use the method of dynamic loading, but can be very strange don't know why, great god give advice or comments please,
Before
Left is this dynamic code, now project
 
Protected override void OnModelCreating (ModelBuilder ModelBuilder)
{
Var modelAssemblies=AppDomain. CurrentDomain. GetAssemblies () Where (a=& gt; A.F ullName. The Contains (" ihs. Models "));

If (modelAssemblies. The Count () & gt; 0)
{
The foreach (var modelAssembly modelAssemblies) in
{
Try
{
Var models=modelAssembly. The GetTypes ();
The foreach (var model in models)
{
Var attr=model. GetCustomAttributes (typeof (SchemaAttribute), true);
If (attr? . Length> 0)
{
Var schemaattr=attr. First () as SchemaAttribute;
Var schema=schemaattr. Name. ToLower ();//assets
ModelBuilder. Entity (the model). ToTable (model. The Name, schema);
Console. WriteLine ($" the loaded schema: {schema}, table: {model. The Name} ");
}
The else
{
ModelBuilder. Entity (the model). ToTable (model. The Name);
Console. WriteLine ($" the loaded schema: default, the table: {model. The Name} ");
}
}
}
Catch Exception (err)
{
Console. WriteLine (err. Message);
}
}
}

//base. OnModelCreating (modelBuilder);
}

Left before the project code, can be mapped successfully,
 
//PersonnelSchema is a static string
ModelBuilder. Entity (). ToTable (nameof (Education), PersonnelSchema);
  • Related