All the Internet in the examples, where all the properties in the entities are automatic, have never seen anything implemented. But what if I, for example, need to make some kind of calculated field? For example, the current age of the employee.
- if you need a calculated property on the DB side, then you should pay attention to @AlexeyAlybin, otherwise, i.e. on the client side then see my answer. - Bald
|
2 answers
in the class, implement the property you need and set the attribute [NotMapped] to it
Example how i would do it
public class User { public int Id {get;set;} public string FirstName {get;set;} public string LastName {get;set;} public DateTime Birthday {get;set;} [NotMapped] public int Age { //Π·Π΄Π΅ΡΡ ΡΠ΅Π°Π»ΠΈΠ·ΡΠ΅ΡΠ΅ ΡΠ²ΠΎΠ΅ Π²ΡΡΠΈΡΠ»ΡΠ΅ΠΌΠΎΠ΅ ΡΠ²ΠΎΠΉΡΡΠ²ΠΎ } } To implement the same with the Model First approach, this answer recommends creating a partial class in which to create the necessary property and set the corresponding attribute to it.
- At the same time, there will be no changes in the database structure? - Yes Man
- How to do the same with the model first approach? Where in the model designer is it? - Yes Man
- @YesMan updated the answer - Bald
- @Bald, here we must take into account that your changes will still not be visible in the database. Therefore, if you want to create a calculated field that is visible in the database, then your answer is not appropriate. - Alex Krass
- @AlexKrass is a good point, you probably should start from what you really need, you can organize the calculated property on the DB side, then this property will not differ in any way on the ef side - Bald
|
For the calculated field, you need to make a private setter, add the DatabaseGeneratedOption.Computed option and in the migration script set the method for calculating the field. It will look something like this:
//ΠΌΠΎΠ΄Π΅Π»Ρ public class User { public int Id {get;set;} public string FirstName {get;set;} public string LastName {get;set;} public DateTime Birthday {get;set;} public int Age {get; private set;} } // ΠΌΠ°ΠΏΠΈΠ½Π³ ΠΌΠΎΠ΄Π΅Π»ΠΈ public UserMap() { HasKey( t => t.Id ); Property( t => t.Id ) .HasDatabaseGeneratedOption( DatabaseGeneratedOption.Identity ) .IsRequired(); Property( t => t.FirstName ) .HasMaxLength( 100 ) .IsRequired(); Property( t => t.LastName ) .HasMaxLength( 100 ) .IsRequired(); Property( t => t.Birthday ) .IsRequired(); Property( t => t.Age ).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed ); } // ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΡΠ°Π±Π»ΠΈΡΡ Π² ΡΠΊΡΠΈΠΏΡΠ΅ ΠΌΠΈΠ³ΡΠ°ΡΠΈΠΈ public override void Up() { CreateTable( "dbo.User";, c => new { Id = c.Int(nullable: false, identity: true), FirstName = c.String(), LastName = c.String(), Birthday = c.DateTime() }) .PrimaryKey(t =>; t.Id); Sql("ALTER TABLE user ADD Age AS DATEDIFF(YEAR, Birthday, GETDATE())"); } - Will your way work with the Model First approach? - Bald
- @Bald, yes this method will work with the Model First approach. You just need to change the generated database migration script - Alexey Alybin 9:51 am
|