entity-framework-mapping

Auditing a Table with EF Code first

强颜欢笑 提交于 2020-01-15 10:39:28
问题 I have a class say Message like following: public class Message { public int Id{get;set;} public string MessageText{get;set;} public int Sender{get;set;} public DateTime CreatedOn{get;set;} //EDIT:2 public virtual Message RepliedTo{get;set;} public virtual IList<Message> Replies{get;set;} public virtual IList<MessageStatusHistory> History{get;set;} //so on and so forth } Now I want to keep statuses of the Message object like what user marked it as read and when. I created a class

MVC 3 Razor-Partial Validation of Model

此生再无相见时 提交于 2019-12-13 03:56:56
问题 I am currently working on a project in MVC 3 where I am leveraging Entity Framework to persist one data model over two Views which each contain one HTML Form (similar to wizard-based design). Yet after the user fills out the appropriate fields on the first View and submits the form, client-side validation for the entire model is triggered, and validation errors are shown for fields that will not even be available for input until the second View instantiates. I have currently implemented a

EF Code First, map two navigation properties to the same object type

浪子不回头ぞ 提交于 2019-12-09 00:39:09
问题 If I have a User class that has these properties: public Guid UserPreferenceId { get; set; } public virtual DefaultUserPreference UserPreference { get; set; } public Guid SecondaryUserPreferenceId { get; set; } public virtual DefaultUserPreference SecondaryUserPreference { get; set; } How can I get this to make via fluent API? When I try to run this it says: Introducing FOREIGN KEY constraint on table 'Users' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE

EF 6 - Cascade Delete on one to many without backreference

一个人想着一个人 提交于 2019-12-07 00:13:22
问题 I have something like this: public class Gadget { public int Id { get; set; } public string Name { get; set;} public int SuperHeroId { get; set; } } public class SuperHero { public int Id { get; set; } public virtual ICollection<Gadget> Gadgets { get; set; } } Notice that while a Gadget is "owned" by a Superhero (and therefore there's an FK in the database), my domain model does not have a hard reference in that direction. When I delete a superhero I would like to also delete all their

Why does the `DatabaseGeneratedOption.None` exist?

限于喜欢 提交于 2019-12-03 11:04:17
This is used with the Property(() => p).HasDatabaseGeneratedOption() call. Is is perhaps to turn off default DB value generation? Pawel EF uses DatabaseGeneratedOption to figure out what to do with the value of a key column for new entities. If the DatabaseGeneratedOption is Identity EF knows that the value the property is set to can be ignored and that the one that comes from the database should be used. If the DatabaseGeneratedOption is None EF will insert the value of the property to the database as the value of the key column. In Code First - when Code First conventions find an int

What is the best way to re-name a column in edmx?

偶尔善良 提交于 2019-12-02 01:34:32
问题 I am using entity framework 4.0 my approach is Database first approach. what is the best way to rename a column name or change the data type of column in edmx. thanks in advance. 回答1: To rename a column: Rename the column in SQL Management Studio. In the EDMX editor, choose "Update from Database", and complete the wizard. You will get a new column with the updated name, and an error-message saying that the old column is not mapped. Remove the old column. To change the datatype: Update the

How to disable automapping of properties in Entity Framework

扶醉桌前 提交于 2019-12-02 01:12:56
问题 I've decided to use fluent mapping in Entity Framework. My intention was to map everyting by code without any atributes and auto mapping functions. Best way I've found is class EntityTypeConfiguration, that I implement for each entity in my project. Later I add property to one of my entity. This property isn't needed to be persisted. I've expected, that until I add mapping for this property, it will be ignored by database and persistence layer. Unfortunatly it doesn't work that way, and

What is the best way to re-name a column in edmx?

旧街凉风 提交于 2019-12-01 22:43:32
I am using entity framework 4.0 my approach is Database first approach. what is the best way to rename a column name or change the data type of column in edmx. thanks in advance. To rename a column: Rename the column in SQL Management Studio. In the EDMX editor, choose "Update from Database", and complete the wizard. You will get a new column with the updated name, and an error-message saying that the old column is not mapped. Remove the old column. To change the datatype: Update the datatype in SQL Management Studio. In the EDMX editor, choose "Update from Database", and complete the wizard.

How to disable automapping of properties in Entity Framework

一个人想着一个人 提交于 2019-12-01 21:32:17
I've decided to use fluent mapping in Entity Framework. My intention was to map everyting by code without any atributes and auto mapping functions. Best way I've found is class EntityTypeConfiguration, that I implement for each entity in my project. Later I add property to one of my entity. This property isn't needed to be persisted. I've expected, that until I add mapping for this property, it will be ignored by database and persistence layer. Unfortunatly it doesn't work that way, and property is mapped. Only way is to use Ignore method or NotMapped attribute, but I don't want to do it

EF Code First, map two navigation properties to the same object type

时光总嘲笑我的痴心妄想 提交于 2019-11-30 21:19:54
If I have a User class that has these properties: public Guid UserPreferenceId { get; set; } public virtual DefaultUserPreference UserPreference { get; set; } public Guid SecondaryUserPreferenceId { get; set; } public virtual DefaultUserPreference SecondaryUserPreference { get; set; } How can I get this to make via fluent API? When I try to run this it says: Introducing FOREIGN KEY constraint on table 'Users' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.