Entity Framework Code First Using Guid as Identity with another Identity Column

This ended up working for me, Entity Framework 5. Turn off automatic migrations Migrate to create the initial table, no frills Declare the ClusterId as Identity (annotation) [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public override int ClusterId { get; set; } Migrate Declare the pk property Id as Identity after the other one has been updated [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public override Guid … Read more

DbSet.Find method ridiculously slow compared to .SingleOrDefault on ID

Find calls DetectChanges internally, SingleOrDefault (or generally any query) doesn’t. DetectChanges is an expensive operation, so that’s the reason why Find is slower (but it might become faster if the entity is already loaded into the context because Find would not run a query but just return the loaded entity). If you want to use … Read more

EF Code-First One-to-one relationship: Multiplicity is not valid in Role * in relationship

Your model is not a 1:1 association. You can still have many Class2 objects referring to the same one Class1 object. Also, your model doesn’t guarantee that a Class2 referring to a Class1 is also referred back by this Class1 object — Class1 can refer to any Class2 object. How to configure 1:1? The common … Read more

Storing TimeSpan with Entity Framework Codefirst – SqlDbType.Time overflow

[Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete(“Property ‘” + nameof(Duration) + “‘ should be used instead.”)] public long DurationTicks { get; set; } [NotMapped] public TimeSpan Duration { #pragma warning disable 618 get { return new TimeSpan(DurationTicks); } set { DurationTicks = value.Ticks; } #pragma warning restore 618 } Update This is now achievable since EF Core 2.1, using … Read more

Entity Framework code first. Find primary key

You can ask mapping metadata to get names of key properties (there can be more then one): ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; ObjectSet<YourEntity> set = objectContext.CreateObjectSet<YourEntity>(); IEnumerable<string> keyNames = set.EntitySet.ElementType .KeyMembers .Select(k => k.Name); Once you have key names you can use reflection to access their values. As you can see the approach reverts back to … Read more

Entity Framework Code First naming conventions – back to plural table names?

The RTM version of Code First will fully support a cool feature called Pluggable Conventions where you can add or replace the default conventions such as the one you mentioned. Fortunately, what you are looking for is already included in CTP5. You can switch off the pluralizing table names convention with removing PluralizingTableNameConvention convention. This … Read more

How to Specify Primary Key Name in EF-Code-First

If you want to specify the column name and override the property name, you can try the following: Using Annotations public class Job { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Column(“CustomIdName”)] public Guid uuid { get; set; } public int active { get; set; } } Using Code First protected override void OnModelCreating(DbModelBuilder mb) { base.OnModelCreating(mb); mb.Entity<Job>() .HasKey(i => … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)