entity-framework-6

How to mark identity column properly with Entity Framework 6.1?

空扰寡人 提交于 2019-11-29 07:12:57
I've seen many posts and answers regarding how to mark a field as the identity column. Many of them are outdated and are targeting older versions of Entity Framework. Some resources tell me to use an attribute on the field: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } Other resources tell me to add this code to OnModelCreating method: modelBuilder.Entity<User>().Property(u => u.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); Which one should I use? First, second, both, doesn't matter, or

EF code-first: How to load related data (parent-child-grandchild)?

拥有回忆 提交于 2019-11-29 07:05:39
I have this entity: public class DynamicPage { public int PageId { get; set; } public int Order { get; set; } public string MenuText { get; set; } public string MenuHover { get; set; } public int? ParentId { get; set; } public virtual DynamicPage Parent { get; set; } public virtual ICollection<DynamicPage> Children { get; set; } } This entity may have 3 level: Parent -> Child -> Grandchild. How can I load the Parent (level 1) whit all associated children (level 2) and for each child, associated grandchild (level 3) if any? Thanks to help. EF 4.1 feature and syntax: var entity = context.Parents

SQL column default value with Entity Framework

南楼画角 提交于 2019-11-29 06:06:05
I am trying to use Code-First EF6 with default SQL values. For example, I have a "CreatedDate" column/property not null with a default in SQL of "getdate()" How do I represent this in my code Model? Currently I have: <DatabaseGenerated(DatabaseGeneratedOption.Computed)> Public Property CreatedDate As DateTime Will this work, or will I need to use a nullable even though the actual column should be not null, so EF doesn't send a value when it hasn't been set: <DatabaseGenerated(DatabaseGeneratedOption.Computed)> Public Property CreatedDate As DateTime? Or is there a better solution out there? I

Map tables using fluent api in asp.net MVC5 EF6?

为君一笑 提交于 2019-11-29 06:04:53
I am trying to add profile/Membership information into my MVC5 application and adding configuration mappings. I get the following error message: my.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. my.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole

EF6, SQLite won't work without App.config

孤街醉人 提交于 2019-11-29 05:40:31
I'm trying to make a plug in that will use EF6.1 and SQLite for an app where I can't change the App.config so all the configuration and connection string needs to be set in code. I found this answer that looked promising Problems using Entity Framework 6 and SQLite So now I have a configuration class like this: public class CollectionDbConfiguration : DbConfiguration { public CollectionDbConfiguration() { SetProviderServices("System.Data.SQLite"(DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices))); SetProviderFactory("System.Data.SQLite.EF6",

Weird “assembly not referenced” error when trying to call a valid method overload

六月ゝ 毕业季﹏ 提交于 2019-11-29 05:37:22
I'm using method overloading in Assembly A : public static int GetPersonId(EntityDataContext context, string name) { var id = from ... in context... where ... select ...; return id.First(); } public static int GetPersonId(SqlConnection connection, string name) { using (var context = new EntityDataContext(connection, false)) { return GetPersonId(context, name); } } When I try to call the second overload from Assembly B , VS produces the following compile-time error: The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced. You must add a reference to assembly

FindAsync and Include LINQ statements

末鹿安然 提交于 2019-11-29 05:31:59
The code I have got so far works fine public async Task<ActionResult> Details(Guid? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ItemDetailModel model = new ItemDetailModel(); model.Item = await db.Items.FindAsync(id); if (model.Item == null) { return HttpNotFound(); } return View(model); } But I want to include 1 table more and cannot use FindAsync public async Task<ActionResult> Details(Guid? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ItemDetailModel model = new ItemDetailModel(); model.Item = await db.Items

Entity Framework auto increment with starting value

白昼怎懂夜的黑 提交于 2019-11-29 05:11:36
I have a entity as follows [Table("ESS_POS_SE_VERSION")] public class Version { [Key, Column("DETAIL_ID"), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Column("DETAIL_TITILE")] public string DetailKey { get; set; } [Column("DETAIL")] public string Detail { get; set; } } SO Id column is auto increment column, but I need to set its starting value to 1000. How can I do it? Consider creating a custom database initializer. It will be called each time your database is created or recreated. For example: public class MyInitializer : DropCreateDatabaseIfModelChanges

MVC scaffolding does not support Entity Framework 6 or later

风格不统一 提交于 2019-11-29 05:05:22
问题 Just upgraded to Entity Framework 6 to take a look. I'm using MVC4. But i recieve this message when trying to make a controller from a model and context. MVC scaffolding does not support Entity Framework 6 or later 回答1: Thought this could use some expanding :) As mentioned above ASP.NET MVC 4 scaffolding does not support EF6 or higher. This means that an older EF, compatible with MVC 4 will have to be installed. To do this: Open the Package Manager Console: select TOOLS -> Library Package

Entity Framework 6.1.3 Mapping Foreign key to non primary key

风流意气都作罢 提交于 2019-11-29 04:07:35
The goal is to have an API with all the fields from the GravityZone with the name of the zone coming from the Zone table. I've tried several permutations of the following code without success. It's currently coming up with null for the Zone which I'm hoping to get either the name as a string or part of the object. I'm working with existing tables that I'm not able to modify. Models: public partial class Zone { [Key] [Column("ZONE_ID")] public decimal ZoneId { get; set; } [Column("ZONE_CODE")] public decimal ZoneCode { get; set; } [Column("ZONE_NAME")] public string ZoneName { get; set; }