entity-framework-6

How can set a default value constraint with Entity Framework 6 Code First?

佐手、 提交于 2019-12-17 10:47:38
问题 In a legacy app, most string properties can't be null and need to have a default value of string.empty. I know it's possible to do this with migrations, but I'm looking for a way to do this using the fluent configuration interface: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Properties<string>().Configure(c => { c.HasMaxLength(255); if (!c.ClrPropertyInfo.IsDefined(typeof (NullableAttribute), false)) { c.IsRequired(); // I want to set a default value

How to inject UserManager & SignInManager

ⅰ亾dé卋堺 提交于 2019-12-17 10:29:13
问题 I am trying to figure out how to inject UserManager and SignInManager. I have installed Ninject in my application and I am using it in the following manner: Please consider this to be a brand new project. Inside Startup.cs I have the following: public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); app.UseNinjectMiddleware(CreateKernel); } private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Load(Assembly

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key “key”

时间秒杀一切 提交于 2019-12-17 10:02:16
问题 My code is like this namespace DiagnosisApp.Models { public class ProcedurePrice { public int ID { get; set; } public int DepartmentID { get; set; } public int ProcedureID { get; set; } public int InsuranceProviderID { get; set; } public int ProcedureCategoryID { get; set; } public int ProcedureSubCategoryID { get; set; } [ForeignKey("ID")] public virtual Department Department { get; set; } [ForeignKey("ID")] public virtual InsuranceProvider InsuranceProvider { get; set; } [ForeignKey("ID")]

Set Command Timeout in EF 6

倾然丶 夕夏残阳落幕 提交于 2019-12-17 09:59:20
问题 I want to set command timeout for query execution, currently I am doing context.Database.CommandTimeout = 90; but i feel this is not working, I tried checking the process logs in database but found the time difference was always less than 90sec. Can someone help how can I set the database timeout in Entity Framework 6? 回答1: Check this : Entity Framework 6 : this.context.Database.CommandTimeout = 180; Entity Framework 5: ((IObjectContextAdapter)this.context).ObjectContext.CommandTimeout = 180;

Eager , Lazy and explicit loading in EF6

女生的网名这么多〃 提交于 2019-12-17 07:16:21
问题 I have read this tutorial and this article but I don't understand exactly the use of each loading type. I Explain I have this POCO : public partial class dpc_gestion { public dpc_gestion() { this.ass_reunion_participant = new HashSet<ass_reunion_participant>(); this.dpc_participant = new HashSet<dpc_participant>(); this.dpc_reunion = new HashSet<dpc_reunion>(); } public int dpc_id_pk { get; set; } public Nullable<int> dpc_id_gdp_fk { get; set; } public Nullable<int> dpc_id_theme { get; set; }

Application can't scaffold items

隐身守侯 提交于 2019-12-17 07:16:10
问题 I created an MVC 5 application in VS 2013 Professional and then used EF 6.1 code first with an existing DB on SQL Server Express. When I try to create the views I’m using the “New scaffolded item…” then selecting the “MVC 5 controller with views, using Entity Framework.” I select the model and context classes and click OK. Then the following error message appears and no code is created. I’ve uninstalled EF Power Tools with the same error. Error There was an error running the selected code

How to create a table corresponding to enum in EF6 Code First?

大憨熊 提交于 2019-12-17 07:03:50
问题 I've followed MSDN on how to handle enumerations in Code First for EF6. It worked, as supposed to but the field in the created table that refers to the enumerator is a simple int . I'd prefer a second table to be produced, the values of which would follow the definition of the enumerator in C# code. So, instead of only getting a table corresponding to Department in the example on MSDN, I'd also like to see a second table populated by the items from Faculty . public enum Faculty { Eng, Math,

How to create a table corresponding to enum in EF6 Code First?

半世苍凉 提交于 2019-12-17 07:03:28
问题 I've followed MSDN on how to handle enumerations in Code First for EF6. It worked, as supposed to but the field in the created table that refers to the enumerator is a simple int . I'd prefer a second table to be produced, the values of which would follow the definition of the enumerator in C# code. So, instead of only getting a table corresponding to Department in the example on MSDN, I'd also like to see a second table populated by the items from Faculty . public enum Faculty { Eng, Math,

DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState.Modified

不打扰是莪最后的温柔 提交于 2019-12-17 06:21:33
问题 When I am in a detached scenario and get a dto from the client which I map into an entity to save it I do this: context.Entry(entity).State = EntityState.Modified; context.SaveChanges(); For what is then the DbSet.Attach(entity) or why should I use the .Attach method when EntityState.Modified already attaches the entity? 回答1: When you do context.Entry(entity).State = EntityState.Modified; , you are not only attaching the entity to the DbContext , you are also marking the whole entity as dirty

Entity Framework 6: audit/track changes

巧了我就是萌 提交于 2019-12-17 05:34:25
问题 I have my core project in C#. I work on a database, where some tables have the columns "user_mod" and "date_mod" for sign who and when made some mods and the same with "data_new" and "user_new". My question: is there a way to centralize this and make this data inserted automatically, where I create the instance of dbContext ? If not, I will use an audit trail tool. I have seen some of these, but there is a problem: all of these, require some code in my model. But I don't want to write in my