entity-framework-4.1

Entity Framework 4.1 Code First - Define many-to-many using data annotations only

﹥>﹥吖頭↗ 提交于 2019-12-12 08:34:25
问题 Is it possible to define a many-to-many relationship in Entity Framework 4.1 (Code First approach) using Data Annotations only, without model builder? For example, something like: Product = { Id, Name, ... } Category = { Id, Name, ... } ProductCategory = { ProductId, CategoryId } You get the picture. I don't want to have an intermediate entity ProductCategory in the context with two many-to-ones since I don't have any additional data, just the two FKs. Also, I should be able to define table

Why the Left Outer join?

≯℡__Kan透↙ 提交于 2019-12-12 08:28:38
问题 weird one. (Probably not weird, at all) I have 3 objects, Employee, Rota and Department. public class Employee { public int Id { get; set; } public String Name { get; set; } public virtual Department Department { get; set; } } internal class EmployeeMapping : EntityTypeConfiguration<Employee> { public EmployeeMapping() { HasKey(a => a.Id); Property(a => a.Id).HasColumnName("UserId"); HasRequired<Department>(a => a.Department).WithOptional().Map(a => a.MapKey("DepartmentId")); } } public class

How do I save a child entity in EntityFramework 4?

耗尽温柔 提交于 2019-12-12 08:10:26
问题 I have a 1-1 relationship between Orders and Contact. i.e. Contact.OrderId references Orders and is also a PK. So I have an existing Order and I add a new Contact to it like so... order.Contact = new Contact() { EmailAddress = "hello" }; context.Orders.Attach(order); context.SaveChanges(); A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship. So what am

MVC .Net Cascade Deleting when using EF Code First Approach

耗尽温柔 提交于 2019-12-12 07:47:22
问题 I'm pretty new to MVC and i'm having troubles with cascade deleting. For my model I the following 2 classes: public class Blog { [Key] public int Id { get; set; } [Required] public string Name { get; set; } [DisplayFormat()] public virtual ICollection<BlogEntry> BlogEntries { get; set; } public DateTime CreationDateTime { get; set; } public string UserName { get; set; } } public class BlogEntry { [Key] public int Id { get; set; } [Required] public string Title { get; set; } [Required] public

Reload an entity and all Navigation Property Association- DbSet Entity Framework

戏子无情 提交于 2019-12-12 07:06:42
问题 I have a problem with entity association refresh. When I get an entity with like this: MyContext context = new MyContext(); Person myPerson = context.PersonSet.FirstOrDefault(); String myPersonName = myPerson.Name; Address myPersonAddress = myPerson.Address; I got an a person with an association named Address and a property named Name. If I modify manually the datas in database for example the property Name, I have to use the following code to reload my entity: context.Entry(myPerson).Reload(

Implementing this generic repository, unit of work and repository factory

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 05:59:20
问题 I have modifed this a bit, removed one method, from the original post where I found this example. Here is the generic repository. /// <summary> /// Repository base class used with DbContext Originally From http://dotnetspeak.com/index.php/2011/03/repository-pattern-with-entity-framework/ /// </summary> /// <typeparam name="TContext">Type of DdContext that this repositiory operates on</typeparam> public class RepositoryBase<TContext> : IDisposable, IRepositoryBase where TContext : DbContext,

Entity Framework Reverse Engineer using Power Tools - No Primary Keys

*爱你&永不变心* 提交于 2019-12-12 05:17:18
问题 I've used the Entity Framework Power Tools Beta 2 to reverse engineer my model classes. However no primary keys have been declared in the data annotations. Is there a way to do this? 回答1: By default, the mapping done via fluent. If you want to change that, you can customize the TT files used to generate the model. Where you would remove it from the mapping.tt and then add the data annotation to entity.tt. Rowan Miller made a great post about that. 来源: https://stackoverflow.com/questions

Relationship mapping table with additional properties?

点点圈 提交于 2019-12-12 05:02:34
问题 I have three classes: public partial class Student : Contact { //Inherited from Contact: //public int ContactId { get; set; } //public string FirstName { get; set; } //public string LastName { get; set; } public virtual StudentExam StudentExam { get; set; } } public partial class Exam { public int ExamId { get; set; } public string Title { get; set; } public DateTime Date { get; set; } public virtual StudentExam StudentExam { get; set; } } public partial class StudentExam { public byte Score

WCF Data service $select returning 'Not Implemented' exception

99封情书 提交于 2019-12-12 04:35:42
问题 I have a project I am working on that has a Order entity that has a navigation property for OrderITems: public class Order { public int OrderId { get; set; } public int CustomerId { get; set; } public virtual ICollection<OrderItem> OrderItems { get; set; } } public class OrderItem { public int OrderId { get; set; } public int ProductId { get; set; } public int Quantity { get; set; } public virtual Order Order { get; set; } } If I query the OrderItems like so: http://mysvc.com/mysvc/OrderItems

How to create dynamic database in entity framework with specified path?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 03:09:58
问题 currently i create dynamic database using CreateDatabase() method in Entity Framework. it executes sucessfully and it creates database at following path: "c:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA" but i want to create database in my application's directory. how to do this? please help me. 回答1: Normally all databases are created on default path configured in SQL Server. Only if you are using SQL Server Express and if you define connection string which specifies