Click here to Skip to main content
15,943,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have a problem with the change tracker in EntityFrame work.
How can I get the Database name from the change tracker?
I implemented my code but It returns just the model name, not the Physical DB name.
Thank you.

What I have tried:

Get physical Db name by change tracker in entityframework
Posted
Updated 1-Oct-20 4:42am
Comments
Sandeep Mewara 1-Oct-20 6:08am    
Not to sure what you are looking for, is it:
foreach (var entityType in dbContext.Model.GetEntityTypes())
{
    var tableName = entityType.GetTableName();
    foreach (var propertyType in entityType.GetProperties())
    {
        var columnName = propertyType.GetColumnName();
    }
}
Moses_k 1-Oct-20 6:41am    
I'm using Asp.net MVC5 and my dbcontext does not have Model.
I just want to show Physical Table Name of current model.
Sandeep Mewara 1-Oct-20 11:06am    
What do you mean you don't have DBContext? Read this: https://docs.microsoft.com/en-us/ef/ef6/fundamentals/working-with-dbcontext
Moses_k 3-Oct-20 1:31am    
Hi,
No, I have Dbcontext for Identity MVC5.

RoleBaseDbContext db = new RoleBaseDbContext();
var s=ContextExtensions.GetTableName<string>(db);

foreach (var ent in this.ChangeTracker.Entries().Where(p => p.State == EntityState.Added || p.State == EntityState.Deleted || p.State == EntityState.Modified))
{

// For each changed record, get the audit record entries and add them
foreach (All_Log x in GetAuditRecordsForChange(ent, userId))
{
this.All_Log.Add(x);
}
}
Moses_k 3-Oct-20 1:32am    
but I think it dose not exist dbContext.Model.GetEntityTypes().
for asp.net mvc5 it exist in asp .net core.

1 solution

"Change tracking" is not relevant to your question.

The dbContext has a .DataBase "class" property.

The Database class points to a .DbConnection class / object.

The .DbConnection class has a .Database "string" that is the "physical db name".
 
Share this answer
 
Comments
Sandeep Mewara 1-Oct-20 11:05am    
OP is looking for the table name corresponding to entities. He wrote database instead of table in original post but later in comment said: "I just want to show Physical Table Name of current model."

I am confused on what he means he doesn't have a model for DBContext given he is working on ASP.NET MVC.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900