Entity Framework 6 GUID as primary key: Cannot insert the value NULL into column ‘Id’, table ‘FileStore’; column does not allow nulls

In addition to adding these attributes to your Id column: [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } in your migration you should change your CreateTable to add the defaultValueSQL property to your column i.e.: Id = c.Guid(nullable: false, identity: true, defaultValueSql: “newsequentialid()”), This will prevent you from having to manually touch your database … 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

How to filter “Include” entities in entity framework?

There are two ways to filter include Entity. Using a projection (See @Eldho answer) Using a third party library Disclaimer: I’m the owner of the project Entity Framework Plus The EF+ Query IncludeFilter allows to easily filter included entities. context.Entry(hotel) .Collection(x => x.Rooms) .Query() .IncludeFilter(y => y.Reservations .Where(z => z is ExecutiveSuite && z.Reservations.Any()) .Load(); … Read more

How to connect to LocalDB in Visual Studio Server Explorer?

In Visual Studio 2012 all I had to do was enter: (localdb)\v11.0 Visual Studio 2015 and Visual Studio 2017 changed to: (localdb)\MSSQLLocalDB as the server name when adding a Microsoft SQL Server Data source in: View/Server Explorer/(Right click) Data Connections/Add Connection and then the database names were populated. I didn’t need to do all the … Read more

Clone Enity framework POCO

This is an example of an extension method I wrote to populate the common values of two objects. public static void PopulateInto( this object source, object target, BindingFlags flags) { foreach (var pi in source.GetType().GetProperties(flags)) { var tpi = target.GetProperty(pi.Name, flags); if (tpi.IsNotNull()) { try { object originalValue; object value = originalValue = source.GetValue<object>(pi.Name, new … Read more