entity-framework-6

EF 6 IsRequired() allowing empty strings

半世苍凉 提交于 2019-12-05 12:26:04
问题 In past projects with versions of EF5 and EF4, the IsRequired() fluent API method would thrown a DbEntityValidationException if the property was null or an empty string. In my current project utilizng EF6, The DBEntityValidationException is not thrown when the string property is empty. Entity: public class Application : BaseEntity { public string Name { get; set; } // navigation properties public IList<Role> Roles { get; set; } } Configuration: internal class ApplicationMapping :

How to add new entity properties in Entity Framework without changing database model

放肆的年华 提交于 2019-12-05 11:56:04
I am new to Entity Framework. I started with database first approach which created my classes corresponding to the tables I selected. I am using MVC . One of my tables has a Date column in it. My requirement is I want to display the Day in my gridview (Grid MVC) i.e. if Date for that particular record fetched is 10/27/2015, then Day should show Tues. I want to do this without adding an extra column for the day in my database. Is there a way for this. Any help will be greatly appreciated. My model class generated is as below:- public partial class QnsNew1 { public int Id { get; set; } public

Entity Framework Seeding with Identity (Microsoft.Owin.Security) user

元气小坏坏 提交于 2019-12-05 10:32:46
I have a class that seed the database which add 2 users with roles and custom fields. The problem I have is that it save the data in [dbo].[AspNetUsers] instead of [dbo].[IdentityUsers]. Both tables are created. When seeded, the data go into AspNetUser. When I launch the web site, and register a new user, the data go into IdentityUser. Here is the Migration class : internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext> { public Configuration() { AutomaticMigrationsEnabled = true; } protected override void Seed(DatabaseContext context) { base.Seed(context); var

Implementing an ICollection<ISomething> with a concrete type to satisfy Entity Framework

二次信任 提交于 2019-12-05 10:26:18
So, this is kind of an obtuse question, but let me see if I can lay it out relatively simply. Lets say I have the following interface: public interface IFoo { ICollection<IBar> Bars { get; set; } } Which I then implement with: public class Foo : IFoo { public virtual ICollection<IBar> Bars { get; set; } } Only Entity Framework can't work with interfaces, so it pretty much completely ignores this navigation property. In order to get EF to recognize it, I need to change it to: public virtual ICollection<Bar> Bars { get; set; } Where Bar would be my implementation of IBar . Only, that fails to

Code First migration from int to Guid primary key issue

痴心易碎 提交于 2019-12-05 08:55:55
I'm trying to change my code first ID column from 'int' to 'Guid', and when trying to run the migration, I get the message: Identity column 'CustomFieldId' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable. I'm defining the column like this: public partial class CustomField : BaseEntity { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid CustomFieldId { get; set; } Mapping it in CustomFieldMapping.cs like this: public CustomFieldMapping() { //Primary key HasKey(t => t.CustomFieldId); /

MySql Connector with EF 6

一笑奈何 提交于 2019-12-05 08:33:43
I am having a weird issue with MySql Connector(6.8.3) and EF6. I was working on a WebApi project where I use MySql and EF6 with Database first approach. Everything worked fine[even deployed on one of the test servers] until I changed the database from 'Test' database to 'Production' database [just the database name] in the connection string and updated the model[just to see nothing is broken!]. After that, it failed to connect to database. So, I changed the connection string back and rebuilt the solution, then I got bunch of 'Mapping' warnings. I deleted the model and tried to create the model

Non Clustered Primary Key Entity Framework Code First

落爺英雄遲暮 提交于 2019-12-05 08:28:42
In Entity Framework Code First approach, can we define the Primary Key as non-clustered index and a combination of few other fields as clustered index. Thanks Larry EntityTypeConfiguration does not provide a means of setting the Primary Key as a non-clustered index, but you can complete this by altering the initial migration used for table creation. There is an example here . Here is an example of how to specify a clustered multiple-column index using attributes: [Index("IX_ColumnOneTwo", 1, IsClustered = true)] public int ColumnOne { get; set;} [Index("IX_ColumnOneTwo", 2, IsClustered = true)

How to access context after each Entity Framework db migration

倾然丶 夕夏残阳落幕 提交于 2019-12-05 06:38:35
When I Add-Migration, I get the appropriate DbMigration class with the Up / Down methods, where I am able to make schema changes and (with the use of the Sql() method) can make data/content changes as well. I'd like to be able to make content changes per migration using the database context. I understand that I could use the Seed method in a Configuration class, but my understanding is that I can only wire up one Configuration with my initializer. I'd prefer to have a UpCompleted()/DownCompleted() methods that would provide access to the db context after the migration completed. This would

Insert Record in Temporal Table using C# Entity Framework

℡╲_俬逩灬. 提交于 2019-12-05 06:19:13
问题 I'm having issue in Insertion of data in Temporal table using C# Entity Framework The Table schema is CREATE TABLE People( PeopleID int PRIMARY KEY NOT NULL, Name varchar(50) Null, LastName varchar(100) NULL, NickName varchar(25), StartTime datetime2 GENERATED ALWAYS AS ROW START NOT NULL, EndTime datetime2 GENERATED ALWAYS AS ROW END NOT NULL, PERIOD FOR SYSTEM_TIME (StartTime,EndTime) ) WITH (SYSTEM_VERSIONING = ON(HISTORY_TABLE = dbo.PeopleHistory)); I created an EDMX asusal and I tried to

How to add EntityObject Generator in Visual Studio 2015?

烂漫一生 提交于 2019-12-05 06:11:19
I have installed the latest Entity Framework version (EntityFramework.6.1.3) in VS2015. I have added the EF in the project and deleted the 2 .tt files ( edmx_file_name.tt and edmx_file_name.Context.tt ) as they were from the DBContext. Now I am trying to add the EF 6.x code generation template by open the model in the EF Designer, right-click on the design surface and select Add Code Generation Item . To add the Code Generation in ObjectContext i need to install the EF 6.x EntityObject Generator. I downloaded it from here . But it I couldn't install it in VS 2015. How do I solve this? Since