ef-code-first

Id of newly added Entity before SaveChanges()

十年热恋 提交于 2019-12-08 15:11:22
问题 I am adding an entity to my database like so: public TEntity Add(TEntity entity) { return (TEntity)_database.Set<TEntity>().Add(entity); } However, the DbSet.Add method isn't returning the newly added entity with the Id etc, it is simply returning the object I passed in to my Add method. Should it be passing me back the complete new entity including Id , or is it not possible to get the Id before SaveChanges() is called? 回答1: All the call to Add() is doing is effectively registering it with

Have a custom Setter on property for EF Code First model entity

我的未来我决定 提交于 2019-12-08 14:58:36
问题 Is it possible to override or add code to setter of property for entity model object in EF Code First approach. e.g. public class Contact { [Key] public int Id { get; private set; } public string FirstName { get; set; } public string LastName { get; set; } public string JobTitle { get; set { // eg. proper case the job title } } } I had tried having a public property marked NotMapped and this set/get to private/protected property that is. But it seems that the property has to public to be

Returning from a DbSet 3 tables without the error “cannot be inferred from the query”

帅比萌擦擦* 提交于 2019-12-08 13:44:41
问题 I have 3 classes. WorkoutSession, WorkoutSessionExercise and Exercise. I would like to return the WorkoutSession with its list of WorkoutSession with the Exercise related. WorkoutSession has many WorkoutSessionExercise, and these one has only a single 1 to 1 relationship with Exercise. var query = from workoutSession in DatabaseContext.SetOwnable<WorkoutSession>() from workoutSessionExercises in workoutSession.WorkoutSessionExercises from exerciseInfo in workoutSessionExercises.Exercise where

Entity Framework Code-First - Define the Non-clustered key for this EntityType

百般思念 提交于 2019-12-08 13:42:51
问题 I Have defined an entity class with a key, but I don,t want that key be cluster because I have another cluster index in my entity, but I received an error when I add an Index attribute to my model, Can anybody tell me that how can I define a Non-Clustered Key ???? 回答1: AFAIK it is still not possible to create non-clustered primary key fields as part of your code first configuration. However, if you are using migrations or are in a position where you are able to, see the following Entity

How to loop through DataAnnotation’s DisplayName in MVC view?

◇◆丶佛笑我妖孽 提交于 2019-12-08 13:26:48
问题 I want to access DataAnnotation ’s DisplayName and similar GroupName of a model class then loop through in MVC view. For Example let me say one of my model properties are like this public class Person { [Display(Name="Home Phone",GroupName="Home")] public string HomePhone { get; set; } [Display(Name = "Home Address", GroupName = "Home")] public string HomeAddress { get; set; } [Display(Name = "Office Phone", GroupName = "Office")] public string OfficePhone { get; set; } [Display(Name =

EF Code first - Detect one-to-many relationships property for given type

泄露秘密 提交于 2019-12-08 13:13:36
问题 We know that we do not need to configure for one-to-many relationships using Data Annotations, one-to-many relationship configured by convention. In following example ICollection<Student> Students is an relationship property public class Student { public Student() { } public int StudentId { get; set; } public string StudentName { get; set; } public virtual Standard Standard { get; set; } } public class Standard { public Standard() { Students = new List<Student>(); } public int StandardId {

How to Map many one-to-many relationship in ASP.NET MVC?

白昼怎懂夜的黑 提交于 2019-12-08 12:04:20
问题 I have few Domain Models - Address , Customer , Employee , StoreLocation . Address has many to one relationship with Customer and Employee and one to one relationship with StoreLocation. public class Address { public int Id; public string Line1 { get; set; } public string Line2 { get; set; } public string Line3 { get; set; } } public class Customer { public int Id { get; set; } public string Name { get; set; } public IList<Address> Addresses { get; set; } } public class StoreLocation { public

Entity Framework Code First - Firebird migration: No MigrationSqlGenerator?

自作多情 提交于 2019-12-08 11:42:47
问题 I'm searching for a way to update my firebird-database. I tried using migrations: private void btnUpdateDb_Click(object sender, EventArgs e) { DbConnection userDBConnection = ClassBasicRepository.GetDBConnection(); var configuration = new Configuration(); configuration.TargetDatabase = new DbConnectionInfo( userDBConnection.ConnectionString, "FirebirdSql.Data.FirebirdClient"); DbMigrator migrator = new DbMigrator(configuration); migrator.Update(); } DbMigrationsConfiguration: public sealed

Why is Entity Framework navigation property null?

时间秒杀一切 提交于 2019-12-08 11:09:10
问题 I am use code first model with a relationship below public class ApplicationUser : IdentityUser { public string UserFirstName { get; set; } public string UserLastName { get; set; } public string UserSchool { get; set; } public UserProfileData UserProfileData { get; set; } public int? MedicalSpecialtyId { get; set; } public virtual MedicalSpecialty MedicalSpecialty { get; set; } // public int? AnalyticsDataId { get; set; } // public ICollection<AnalyticsData> AnalyticsDatas { get; set; } }

Help implementing simple / custom MembershipProvider

谁都会走 提交于 2019-12-08 10:11:50
问题 I have been follow Steve Sandersons MVC2 book and have implemented a simple / custom MembershipProvider. You will not that a valid user has been hardcoded. My question is how do I get this to validate against my "Profiles" SQLServer table? PS - I am using EF 4.1 Code First Please see below: public class Profile { [Key] public int UserId { get; set; } [Required] public string UserName { get; set; } [Required] public string Password { get; set; } } public class SimpleMembershipProvider :