entity-framework-4

How can I save an enumeration value to a database?

人盡茶涼 提交于 2020-01-23 09:29:09
问题 For example: namespace PizzaSoftware.Data { public class User { public string Username { get; set; } public string Password { get; set; } public Permission PermissionType { get; set; } } public enum Permission { Basic, Administrator } } If if I were to use Entity-Framework's CodeFirst, how can I save this value? This is for a Windows Forms, Desktop application. Thank you! 回答1: You can retrieve the int value (or whatever type you have your enum set to) via a simple cast and save that to the DB

How do you do web forms model validation?

拈花ヽ惹草 提交于 2020-01-22 16:59:31
问题 We have an application with three layers: UI, Business, and Data. The data layer houses Entity Framework v4 and auto-generates our entity objects. I have created a buddy class for the entity VendorInfo : namespace Company.DataAccess { [MetadataType(typeof(VendorInfoMetadata))] public partial class VendorInfo { } public class VendorInfoMetadata { [Required] public string Title; [Required] public string Link; [Required] public string LinkText; [Required] public string Description; } } I want

What effect do the different EF 4 SaveOptions have on the ObjectContext?

冷暖自知 提交于 2020-01-22 08:54:07
问题 I'm trying to resolve an error very similar to the one outlined here: InvalidOperationException when calling SaveChanges in .NET Entity framework It appears that the solution (which I have not tried yet, admittedly) is to pass System.Data.Objects.SaveOptions.None as the SaveOptions parameter for the SaveChanges() method. So before I do that, I'm trying to understand exactly how the different SaveOptions work (None, AcceptAllChangesAfterSave, DetectAllChanges). I haven't been able to find a

Entity Framework Code First: How can I determine the foreign key property used for a navigation property at runtime?

蓝咒 提交于 2020-01-22 07:53:40
问题 I have an Entity Framework Code First DbContext with the following entities configured. In this example class Bar is a child of class Foo. public class Foo { public Guid Id { get; set; } public virtual ICollection<Bar> Bars { get; set; } } public class Bar { public Guid Id { get; set; } public Guid FooId { get; set; } public virtual Foo Foo { get; set; } } Now I know that internally, Entity Framework understands that the relationship between Foo and Bar is defined by the foreign key Bar.FooId

Entity Framework ObjectContext with Dependency Injection

别说谁变了你拦得住时间么 提交于 2020-01-21 10:28:54
问题 Well, it seems like I'm stuck in my application structure. Here's what I want to do: UI layer: An ASP.NET webforms website. BLL: Business logic layer which calls the repositories on DAL. DAL: .EDMX file (Entity Model) and ObjectContext with Repository classes which abstract the CRUD operations for each entity. Entities: The POCO Entities. Persistence Ignorant. Generated by Microsoft's ADO.Net POCO Entity Generator. I'd like to create an obejctcontext per HttpContext in my repositories to

Entity Framework ObjectContext with Dependency Injection

Deadly 提交于 2020-01-21 10:28:45
问题 Well, it seems like I'm stuck in my application structure. Here's what I want to do: UI layer: An ASP.NET webforms website. BLL: Business logic layer which calls the repositories on DAL. DAL: .EDMX file (Entity Model) and ObjectContext with Repository classes which abstract the CRUD operations for each entity. Entities: The POCO Entities. Persistence Ignorant. Generated by Microsoft's ADO.Net POCO Entity Generator. I'd like to create an obejctcontext per HttpContext in my repositories to

How to determine if Navigation Property in the Entity Framework is set without loading the related records

爱⌒轻易说出口 提交于 2020-01-21 04:23:26
问题 I'm not sure about Navigational Properties in EF 4 so I would kindly ask you an explanation. Lets imagine this scenarios: A) I have two Entities A and B with a relation N to N (many to many) and tree Table in my Data Base A and B and a Link Table AB with two Foreign Key . In this scenario EF create a Navigational Property lets call it X and also a XReference . B) I have two Entities A and B with a relation 1 to N (one to many) and two Table in my Data Base A and B with one Foreign Key . In

How do I load related entities of type IEnumerable<T>

只愿长相守 提交于 2020-01-20 08:13:25
问题 I'm trying to load related entities of a SingleOrDefault entity, but I am getting the following exception: The navigation property of type IEnumerable is not a single implementation of type ICollection I've tried doing this several ways and ultimately get the same error above for each query against the context (I've included the other ways in comments). using(var context = CustomObjectContextCreator.Create()) { return context.Job.Include("Surveys").Include("SiteInfoes") .Where(r => r.Jobid ==

Struggling with EF's Execute Commands to get the newest ID after an insert

社会主义新天地 提交于 2020-01-17 08:09:10
问题 I am issuing an Insert statement directly against the DB (SQL Server 2008), using EF6.1 on ASP.NET 4.5. I need to get the newest ID in the transaction post insert, however I cannot quite figure out the code. This is what I have at present: db.ExecuteStoreCommand("INSERT INTO Order ([SupplierId]) Values ({0})", SID); Int32 intId = db.ExecuteStoreQuery<Int32>("SELECT SCOPE_IDENTITY()").First(); var myOrder = db.Order.First(r => r.Id == intId); The above example is simplified. Is it possible

Entity framework 4, update specific properties

血红的双手。 提交于 2020-01-17 05:11:06
问题 I'm used to using code first where I can do: db.Users.Attach(user); db.Entry(user).Property(propertyName).IsModified = true; How do I do this with a DataModel approach? I do not have the Entry method on my datacontext. 回答1: Use: context.Users.Attach(user); ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(user); entry.SetModifiedProperty(propertyName); 来源: https://stackoverflow.com/questions/6947950/entity-framework-4-update-specific-properties