entity-framework

Entity Framework and SQLite, the ultimate how-to

那年仲夏 提交于 2021-02-07 10:33:17
问题 I'm trying to get Entity Framework (6.4.4. the newest version in summer 2020) working together with SQLite (1.0.113.1, also latest in summer 2020). I found a lot of information about how to do this, but this information was not always helpful, quite often they contradicted each other. Now that I found out how to do it, I decided to jot down how I did it. The question describes the classes and the tables, the answer will describe how to do it. I describe a database for Schools, where every

EF Code First Update Database Encountered Error “There is already an object named 'Products' in the database”

南笙酒味 提交于 2021-02-07 10:30:17
问题 I'm using EF5 Code First in MVC4. There are tables where fields have decimal datatypes. I just wanted to update the precision of decimal originally from (18,2) to (18,4) . Please help. To achieve the goal I just modified the OnModelCreating something like this: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<DealerLevelDiscount>().Property(d =>d.Discount ).HasPrecision(18, 4); modelBuilder.Entity<DealerTransaction>().Property(d => d.ProductDiscount)

EntityFramework TPT inheritance

馋奶兔 提交于 2021-02-07 10:20:32
问题 I have a simple TPT Inheritance, EF 6.1.1 public class Base { public int Id {get; set;} } public class Derived : Base { public int SomeProperty {get; set;} } Note that in my case Base is NOT an abstract class, because in my domain can exists an instance of Base without Derived . DbContext : public class ApplicationDbContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Derived >().ToTable("Derived")

EntityFramework TPT inheritance

a 夏天 提交于 2021-02-07 10:19:53
问题 I have a simple TPT Inheritance, EF 6.1.1 public class Base { public int Id {get; set;} } public class Derived : Base { public int SomeProperty {get; set;} } Note that in my case Base is NOT an abstract class, because in my domain can exists an instance of Base without Derived . DbContext : public class ApplicationDbContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Derived >().ToTable("Derived")

Representing a Junction Table in Entity Framework

血红的双手。 提交于 2021-02-07 10:09:52
问题 I am creating a schema where the following logic applies: A String can belong to multiple locations. Multiple Locations can have multiple String , or no String . The DateTime (As DateScraped ) at which the relationship between Location and String was formed must be recorded. Basically, I've mapped the relationship as a many-to-many relationship using a junction table like so: In mapping the chart in Code First EF6 (Using SQLite), I have the following objects: public class Location { [Key,

Representing a Junction Table in Entity Framework

你说的曾经没有我的故事 提交于 2021-02-07 10:09:32
问题 I am creating a schema where the following logic applies: A String can belong to multiple locations. Multiple Locations can have multiple String , or no String . The DateTime (As DateScraped ) at which the relationship between Location and String was formed must be recorded. Basically, I've mapped the relationship as a many-to-many relationship using a junction table like so: In mapping the chart in Code First EF6 (Using SQLite), I have the following objects: public class Location { [Key,

Representing a Junction Table in Entity Framework

泄露秘密 提交于 2021-02-07 10:09:28
问题 I am creating a schema where the following logic applies: A String can belong to multiple locations. Multiple Locations can have multiple String , or no String . The DateTime (As DateScraped ) at which the relationship between Location and String was formed must be recorded. Basically, I've mapped the relationship as a many-to-many relationship using a junction table like so: In mapping the chart in Code First EF6 (Using SQLite), I have the following objects: public class Location { [Key,

Convert a table with different relational values to excel columns

你离开我真会死。 提交于 2021-02-07 09:47:56
问题 I have these tables: Category CategoryId CategoryTitle ... ICollection<Article> Articles Each category can have several articles: Article ArticleId ArticleTitle NumberOfComment NumberOfView ... ICollection<ArticleReview> Reviews And each article has several reviews by some user: ArticleReview ArticleReviewId ReviewPoint ArticleId ReviewerId i am trying to export excel report using EPPlus package Here is my ExcelExport class : public class excelExport { public string ArticleTitle { get; set; }

Scope_Identity() in Entity Framework

泪湿孤枕 提交于 2021-02-07 09:44:55
问题 What is the equal code in EntityFramework to this sql code? select Scope_Identity() I want to get the id of the last record that i have inserted to database in EF. 回答1: You must ensure that your key property is mapped with StoreGeneratedPattern.Identity (here you can find some more about this setting). This should be used as default when generating the model from MS SQL database where the key column is defined with IDENTITY . After that it is enough to call SaveChanges on the context and the

Generic Unit Of Work

六月ゝ 毕业季﹏ 提交于 2021-02-07 09:26:33
问题 I have implemented EntityFramework pattern along with Repository and Unit Of Work. The implementation is similar to Code Project Repository Example, however I need an enhancement for the Unit Of Work. The unit of work public class GenericUnitOfWork : IDisposable { // Initialization code public Dictionary<Type, object> repositories = new Dictionary<Type, object>(); public IRepository<T> Repository<T>() where T : class { if (repositories.Keys.Contains(typeof(T)) == true) { return repositories