TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ankit Yadav
NA
762
39.4k
Ignore properties in data model while keeping them in EF CoreMigration
Jul 3 2020 8:32 AM
I have properties in my Model like
public
class
Test{
public
int
Id{
get
;
set
; }
public
string
Title {
get
;
set
; }
public
DateTime? CreatedDate {
get
;
set
; }
public
DateTime? ModifiedDate {
get
;
set
; }
public
DateTime? DeletedDate {
get
;
set
; }
}
Here, I am using this Test class for creating Table in Database using Migration, Now the table is created successfully but the problem is when i want do any operation using stored procedure which is like " Select Title from Test where Id=1" ,When i run the this i am facing error like this
The required column 'CreatedDate' was not present in the results of a 'FromSql' operation"
I tried
NotMapped Attribute it works fine but when i add another migration the NotMapped properties gets Dropped from the database after updating the database
Also use Shadow properties and Ignore properties like
protected
override
void
OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Test>().Property<DateTime?>(
"CreatedDate"
);
modelBuilder.Entity<Test>().Property<DateTime?>(
"ModifiedDate"
);
modelBuilder.Entity<Test>().Property<DateTime?>(
"DeletedDate"
);
}
Also try this
protected
override
void
OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Test>().Ignore(x => x.DeletedDate);
modelBuilder.Entity<Test>().Ignore(x => x.IsDeleted);
modelBuilder.Entity<Test>().Ignore(x => x.ModifiedDate); }
But the issue remains the same ,
So the issue is i want to ignore the CreateDate, ModifiedDated, DeletedDated property while performing DB operation and also not want to drop these columns from Database when i add and update new migration. Can anyone guide me what i need to do to resolve this issue.
Reference link: https://stackoverflow.com/questions/47057856/ignore-properties-in-data-model-while-keeping-them-in-ef-core-migrations
Reply
Answers (
4
)
app.config used by DLL
How to create regex in allow anything content using C# ?