ef-code-first

Unique multiple column in EF6 codefirst

假如想象 提交于 2019-12-10 15:45:29
问题 I have a class Email that looks like : public class Email { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string Subject { get; set; } public string Body { get; set; } public string From { get; set; } public DateTime SentOn { get; set; } public List<string> To { get; set; } } To ensure uniqueness I made a compound key on Subject , From and SentOn This created the problem that when Subject excess 128 characters, validation fails. So I just put a

EF Codefirst How to create separate table for derived class?

我是研究僧i 提交于 2019-12-10 15:16:27
问题 I have objects in their own tables using EF Codefirst. Now I try to produce an "archive" for changed objects living in separate tables for each of those objects. For instance: public class Person { [Key] public virtual Guid Id { get; set; } [Required] public string Name { get; set; } } public class Person_Archive : Person { [Key] [Columnn( Order = 1 )] public override Guid Id { get; set; } [Key] [Columnn( Order = 2 )] public DateTime ChangedAt { get; set; } public string ChangedBy { get; set;

Are child entities automatically tracked when added to a parent?

痞子三分冷 提交于 2019-12-10 15:14:35
问题 I want to know whether EF CodeFirst will automatically track "child" objects in the example below. var db = MyDataContext(); var order = db.Orders.Find(orderId); order.AddOrderLine("Fancy Product"); db.Commit(); Here are my (simplified) domain entities public class OrderLine { public Guid OrderLineId { get; private set; } public Guid OrderId { get; private set; } public string Description { get; private set; } public OrderLine(Guid orderId, string description) { OrderLineId = Guid.NewGuid();

Passing query results in a viewbag

て烟熏妆下的殇ゞ 提交于 2019-12-10 15:13:27
问题 This seems like it should be so easy, but I've tried three or four ways to do it (but to no avail). I'm just trying to put a query result in a viewbag and display it. I've tried putting a model object list in a ViewBag: var mesg = from MSG in lemondb.Messages where MSG.msg == Membership.GetUser().ToString() select MSG; ViewBag.messages = MSG; And then I try to spit it out in a .cshtml: var message = (List<LemonTrader.Models.Message>)ViewBag.messages; // <--- fails here because it is a string

Using Singular Table Names with EF Core 2

天大地大妈咪最大 提交于 2019-12-10 14:55:45
问题 I want my domain class name to match my db table name (no pluralisation). In EF Core 1.1, I used this code to do that: public static void RemovePluralisingTableNameConvention(this ModelBuilder modelBuilder) { foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes()) { entityType.Relational().TableName = entityType.DisplayName(); } } In EF Core 2.0, this code doesn't compile as Relational() is not a method on IMutableEntityType. Anyway, in EF Core 2.0, they have added

Ninject scope problems with TopShelf, Ninject and EF code first

岁酱吖の 提交于 2019-12-10 14:44:46
问题 I am currently using TopShelf with Ninject to create a Windows Service. I have the following code to setup the Windows Service using TopShelf: static void Main(string[] args) { using (IKernel kernel = new StandardKernel(new NinjectDependencyResolver())) { Settings settings = kernel.Get<Settings>(); var host = HostFactory.New(x => { x.Service<BotService>(s => { s.ConstructUsing(name => new BotService(settings.Service.TimeInterval)); s.WhenStarted(ms => ms.Start()); s.WhenStopped(ms => ms.Stop(

How to make Entity Framework CTP5 work with SQLite?

十年热恋 提交于 2019-12-10 14:28:34
问题 I am having a very hard time in using the SQLite db with EF CTP5. I was just trying to execute this MSDN example with SQLite. But at the line var food = db.Categories.Find("FOOD"); I am getting a runtime exception: System.Data.SQLite.SQLiteException (0x80004005): SQLite errorno such table: Categories Note: The app.config file has to be modified and is as follows: App.config <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" /> </startup>

entity framework 4.1 two tables with the same property name

南楼画角 提交于 2019-12-10 14:22:38
问题 i'm having some problems with entity framework and a sql database. so my problem is this: in my database there are two tables that have the same property, they are identical in type and length but they are not related in any way. no foreign key whatsoever. as soon as i start to interact with the database entity framework spits out the famous error: error 0019: Each property name in a type must be unique. Property name was already defined. i'm using the code first approach by the way. this is

Generic Repository with Soft Delete Feature

陌路散爱 提交于 2019-12-10 14:19:13
问题 I have a generic repository implementation. I'm using asp.net mvc c#, code first entity framework. I created an interface named ISoftDelete: public interface ISoftDelete { bool IsDeleted { get; set; } } I implemented Delete and GetById in my base repository as follows: public virtual void Delete(T entity) { if (entity is ISoftDelete) { ((ISoftDelete)entity).IsDeleted = true; } else { dbset.Remove(entity); } } public virtual T GetById(long id) { T obj = dbset.Find(id); if (obj is ISoftDelete)

Entity Framework Code First not lazy loading after save

倾然丶 夕夏残阳落幕 提交于 2019-12-10 14:19:07
问题 I have a lookup table and a data table in my db. I'll use gender and person for an example. So let's say the gender table looks like so: Id Code 1 Male 2 Female and the person table looks like so: Id Name GenderId 1 Bob 1 2 Jane 2 I've modelled both tables in EF code first like so: public class Gender { public int Id {get;set;} public string Code {get;set;} } public class Person { public int Id {get;set;} public string Name {get;set;} public int GenderId {get;set;} public virtual Gender {get