How to Ignore Shadow Properties using EF Database-First approach to Scaffold-DBContext and Generate Separate Trackable class

I am using the Entity Framework Database-First approach previously I have used the Code-First approach and created a Tracking class for common properties and inherit all the entities from it as shown below:

public abstract class Trackable
{
    public int Id { get; set; }
    public DateTime? CreatedDateTime { get; set; }
    public DateTime? UpdatedDateTime { get; set; }
    public int? CreatedUser { get; set; }
    public int? UpdatedUser { get; set; }
}

public class User: Trackable
{
    [Key]
    public new int Id { get; set; }
    [Required]
    [StringLength(255)]
    public string UserName { get; set; }
    [Required]
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool IsActive { get; set; }
}

above code generated single table having all the properties exist in User as well as Trackable class.
Now I need to generate the same output(I mean classes) while using Database-First approach.

I have tried but it generated a single User class having all the properties that exist in the table inside the Database.

Can anyone guide me that what should I need to do to achieve the expected result same as in the case of Code-First I have done?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Hi @Muhammad Shahid Khan Afridi,

above code generated single table having all the properties exist in
User as well as Trackable class.

According to your class, I have add the following code in the DbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<User> AppUsers { get; set; }
}

Then, after migration using EF code first, it will create a Users table in the database, like this:

How to Ignore Shadow Properties using EF Database-First approach to Scaffold-DBContext and Generate Separate Trackable class

Can anyone guide me that what should I need to do to achieve the
expected result same as in the case of Code-First I have done?

Now, it seems that you want to generate the related class (with inheritance) using EF Database-first, I assume you are using the ADO.Net Entity Data Model, if that is the case, after generate the related class, as you said, we can only get a single User class in the .edmx file, like this:

How to Ignore Shadow Properties using EF Database-First approach to Scaffold-DBContext and Generate Separate Trackable class

Here you can refer the following steps to change the tables:

First, right click the User entity, and open the Properties panel, change the “Entity Set Name” and “Name” property.(change the name from “Users” to “DBF_Users”, this is used to prevent the Schema specified is not valid. Errors: The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'User' error).

How to Ignore Shadow Properties using EF Database-First approach to Scaffold-DBContext and Generate Separate Trackable class

After that save the changes (click the OK button):

How to Ignore Shadow Properties using EF Database-First approach to Scaffold-DBContext and Generate Separate Trackable class

Then, change the user class as below (remember to add the Table attribute to map the table in the database) and save changes:

How to Ignore Shadow Properties using EF Database-First approach to Scaffold-DBContext and Generate Separate Trackable class

After that you could refer the following code to use the genrested class:

How to Ignore Shadow Properties using EF Database-First approach to Scaffold-DBContext and Generate Separate Trackable class


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x