D
D
Daniel Khaliulin2016-11-06 11:11:18
.NET
Daniel Khaliulin, 2016-11-06 11:11:18

How to create a property that does not need to be stored in the database with the model first approach?

There is a book class generated by model first:

public partial class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Author { get; set; }
        public int Year { get; set; }
        public string Publishing { get; set; }
        public string Discription { get; set; }
        public string Genre { get; set; }
        public int Count { get; set; }
    }

I need to add the property AvailableCount to it, which does not need to be stored in the database and which should collect the number of available books according to different tables. It will only have a getter. Looks something like this:
public int AvailableCount {
                    get
                    {
                        // Логика..
                        int someInt = 12;
                        return someInt;
                    }
                }

And if there is a NotMapped annotation for the Code First approach, then how can I be in Model First?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tsiren Naimanov, 2016-11-06
@ImmortalCAT

[NotMapped]
public int AvailableCount {
    get
    {
        int someInt = 12;
        return someInt;
    }
}

R
Roman, 2016-11-06
@yarosroman

create a separate file

public partial class Book{
        [NotMapped]
        public int AvailableCount {
                    get
                    {
                        // Логика..
                        int someInt = 12;
                        return someInt;
                    }
                }
}

and nothing will be overwritten by you,
or MS SQL has calculated fields, I think it would be more correct to use them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question