entity-framework-4.1

Asp.net mvc entity framework code first associate foreign key with different name

天大地大妈咪最大 提交于 2019-12-12 03:09:24
问题 How can I associate a foreign key with different names here: createdby in post and UserID in users table. public class Post : IValidatableObject { [Key] public long PostID { get; set; } public long? ParentPostID { get; set; } [ForeignKey("CreatedBy")] public virtual User Users { get; set; } [ScaffoldColumn(false)] public DateTime? CreatedDate { get; set; } [ScaffoldColumn(false)] public long? CreatedBy { get; set; } } public class User { [Key] public long UserID { get; set; } [Required]

Can I execute a scalar query in EF 4.1?

試著忘記壹切 提交于 2019-12-12 03:09:22
问题 Is it possible to execute a scalar-valued query using EF 4.1? I've tried: ObjectQuery<int> query = new ObjectQuery<int>("select count(*) from myTable", _context); var result = query.Execute(MergeOption.NoTracking); return result.FirstOrDefault(); but it returns an error: The query syntax is not valid. Near term '*' Is the only way to execute a scalar-valued query to call a stored proc? 回答1: It seems to me that you are thinking of entity framework as an "ADO.net 2.0" rather than the ORM that

Entity Framework Code First - Tests Overlapping Each Other

假如想象 提交于 2019-12-12 02:52:57
问题 My integration tests are use a live DB that's generated using the EF initalizers. When I run the tests individually they run as expected. However when I run them all at once, I get a lot of failed tests. I appear to have some overlapping going on. For example, I have two tests that use the same setup method. This setup method builds & populates the DB. Both tests perform the same test ACT which adds a handful of items to the DB (the same items), but what's unique is each test is looking for

Does it make sense to create new object in Entity Framework before saving it to resolve problems with multiple Contexts?

你说的曾经没有我的故事 提交于 2019-12-12 02:27:18
问题 Does it make sense to create new object before trying to save object that I already have loaded but with more then one Context? After lots of tries and errors this code seems to work fine. Basically it's regetting values for Konsultanci and Szkolenie with the current Context based on objects that are read from ObjectListView. var listaDoPodmiany = new List<SzkolenieUczestnicy>(); if (listaUczestnikow.GetItemCount() > 0) { foreach (SzkolenieUczestnicy currentUczestnik in listaUczestnikow

Issue with Entity Framework/db relations

二次信任 提交于 2019-12-12 01:54:15
问题 I have a class Article: public class Article { public int Id { get; set; } public string Text { get; set; } public Title Title { get; set; } } And Title: public class Title { public int Id { get; set; } public string Name { get; set; } public int MaxChar { get; set; } } Before you can write an Article , you have to choose your Title from a list, so your StringLength for Article.Text can be determined. Meaning, this article can only have a certain amount of chars, deppending on what 'Title'

Entity Framework Code First Update Does Not Update Foreign Key

走远了吗. 提交于 2019-12-12 01:53:45
问题 I'm using EF 4.1 Code First. I have an entity defined with a property like this: public class Publication { // other stuff public virtual MailoutTemplate Template { get; set; } } I've configured this foreign key using fluent style like so: modelBuilder.Entity<Publication>() .HasOptional(p => p.Template) .WithMany() .Map(p => p.MapKey("MailoutTemplateID")); I have an MVC form handler with some code in it that looks like this: public void Handle(PublicationEditViewModel publicationEditViewModel

Entity Framework code first relationships and navigation property?

浪尽此生 提交于 2019-12-12 01:52:53
问题 i have the following classes public class Subject{ public int SubjectId { get; set; } public String SubjectName { get; set; } public String SubjectCategory { get; set; } } public class QuestionDescriptor { public int QuestionDescriptorId { get; set; } public String QuestionText { get; set; } public String Answer { get; set; } public int SubjectId { get; set; } public virtual Subject Subject { get; set; } } i have configured it using the following code ,i want that a Subject can have many

Why is one of my reference properties not being included when using LINQ to Entities Include()?

吃可爱长大的小学妹 提交于 2019-12-11 23:17:12
问题 I have the following property declarations and mappings (abridged classes): public class Consumer: AuditableEntity { public virtual Consumer Parent { get; set; } public virtual User User { get; set; } public virtual Role Role { get; set; } public virtual ICollection<Consumer> Consumers { get; set; } } public ConsumerMap() { HasRequired(t => t.Role) .WithMany(t => t.Consumers) .Map(a => a.MapKey("RoleId")) .WillCascadeOnDelete(false); HasRequired(t => t.User) .WithOptional(t => t.Consumer)

Entity Framework Code First wrapper or repository?

无人久伴 提交于 2019-12-11 23:06:01
问题 I've seen it mentioned sometimes that Repository Pattern is built into Entity Framework Code First via the DbSet and DbContext objects. However that leaves a few problems: 1) Injection - Hard to inject as there isn't a clear cut Interface 2) Mocking - Same as above 3) Multiple references to EnitityFramework.dll - Let's say I create my Code First in it's own assembly/project and then want to reference that in another place I also have to reference entityFramework.dll without some wrapper

VS2012 Entity Framework Error - Unable to load the specified metadata resource

假如想象 提交于 2019-12-11 22:24:30
问题 I am using VS2012. When I go an create a .edmx file, it creates the following structure: ->Model.edmx -->Model.Context.tt -->Model.Designercs -->Model.edmx.diagram -->Model.tt From my understanding, VS2012 generates T4 templates (the .tt files) which generate the entities and context. The default entities generated in VS2012 are POCO entities. I am able to add a table to the Model just fine. When I do a compile, I get no errors. When I do the following: Model dbContext3 = new Model(); var