entity-framework-4.1

EF-Code first complex type with a navigational property

旧街凉风 提交于 2019-12-01 02:53:59
My Model: public class Country { public int CountryId { get; set; } public string Name { get; set; } public virtual ICollection<User> Users { get; set; } } public class Location { public string Address { get; set; } public virtual int CountryId { get; set; } public virtual Country Country { get; set; } } public class User{ protected User() { Location = new Location(); } public int UserId { get; set; } public Location Location { get; set; } } When generating the database, I get: One or more validation errors were detected during model generation: System.Data.Edm.EdmEntityType: : EntityType

Is it possible to directly reference a many-to-many table using entity framework, code first

。_饼干妹妹 提交于 2019-12-01 02:33:05
I am using the entity framework and modelling a many-to-many relationship. I have created the relationship between the two entities using the fluent API (let's say users and groups): this.HasMany(t => t.Users) .WithMany(t => t.Groups) .Map( m => { m.ToTable("GroupMembers"); m.MapLeftKey("Group_Id"); m.MapRightKey("User_Id"); }); This works great, but I'd like to also be able to reference the GroupMembers table directly. To do that, I have something like: [Table("GroupMembers")] public class GroupMember { #region Properties /// <summary> /// Gets or sets the group. /// </summary> public virtual

Entity Framework 4.1 Database First does not add a primary key to the DbContext T4 generated class

倾然丶 夕夏残阳落幕 提交于 2019-12-01 01:25:56
问题 I am just getting started with Entity Framework 4.1, trying out the "database first" mode. When EF generates a Model class with the "ADO.Net DbContext Generator," shouldn't it identify the primary key for the class with a [Key] attribute? Without this, it appears incompatible with the T4 MVCScaffolding. Here are the details: Using the Entity Data Model Designer GUI, I have added a simple "country" table to the model from my existing database. The GUI correctly identifies a single integer

Date field giving required error on validation

限于喜欢 提交于 2019-12-01 00:45:40
问题 I have created a model in my asp.net MVC 3 website and have a property named DateOpened: [Column("Date Opened")] [Display(Name = "Date Opened:")] [DataType(DataType.Date)] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public DateTime DateOpened { get; set; } I didn't apply [Required] data annotation to it, but when I try to save the form, It says required field. In database it is null. Please suggest solution. 回答1: That's normal. DateTime is a value type

MVC3 Action Filter Using Database (EF 4.1 DBContext, Ninject)

巧了我就是萌 提交于 2019-12-01 00:41:04
I'm trying to setup an 'Authorization' Filter on an Action, creating my own ActionFilterAttribute where I do a database lookup to determine if a user has access to a certain resource. On my class inheriting from ActionFilterAttribute, I have created an Injected(Ninject) property to hold the service that I am using for the database access. I have a parameterless constructor so that I can use this as an attribute on my actions. In the 'OnActionExecuting' Method, I am able to gain access to the Injected property (it's not null), but the base DBCotext that it is using is closed. This working fine,

DeleteObject method is missing in Entity Framework 4.1

纵然是瞬间 提交于 2019-12-01 00:19:48
This is driving me crazy. I am getting error that object doesn't contain definition for DeleteObject. Here is my line of code that produces an error: ctx.Tanks.DeleteObject(Tank); I tried to reference another object from another edmx file that my friend has created and then everything is fine, DeleteObject exists. I don't think I miss any references in my project. And project itself contains edmx file and I used DBContext to create POCOs. Any ideas? The DbContext API defines DbSet s not ObjectSet s. DbSet has a Remove method not DeleteObject method. You need to first decide which API you are

Difference between DbSet.Remove and DbContext.Entry(entity).State = EntityState.Deleted

被刻印的时光 ゝ 提交于 2019-12-01 00:06:43
问题 Consider the following entity model: public class Agreement : Entity { public int AgreementId { get; set; } public virtual ICollection<Participant> Participants { get; set; } } public class Establishment : Entity { public int EstablishmentId { get; set; } } public class Participant : Entity { public int AgreementId { get; set; } public virtual Agreement Agreement { get; set; } public int EstablishmentId { get; set; } public virtual Establishment { get; set; } public bool IsOwner { get; set; }

Entity Framework foreign keys to non-primary key fields

女生的网名这么多〃 提交于 2019-11-30 23:17:20
问题 Is it possible for Entity Framework 4.0 to have an association/navigation property based off of a foreign key to a non-primary key field (it has a unique constraint). 回答1: No because EF don't understand unique constraint yet and relations in EF must follow same rules as relations in database. Without unique principal relation cannot exist and the only way to get unique principal in EF is using primary key. 来源: https://stackoverflow.com/questions/9217448/entity-framework-foreign-keys-to-non

EF 4.1 RC: Weird Cascade Delete

妖精的绣舞 提交于 2019-11-30 22:44:19
I have to admit, the features of EF 4.1 RC Codefirst, DataAnnotations and FluentAPI are still overwhelming to me. Sometimes I really don't know what I am doing ;-) Please see the following POCOs: public class Country { [Key] public Guid ID { get; set; } [Required] public virtual Currency Currency { get; set; } } public class Currency { [Key] public Guid ID { get; set; } public virtual ICollection<Country> Countries { get; set; } } The general idea: Every country needs to have a currency. But a currency does not need to be assigned to a country at all. If you let EF create the corresponding

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Object

岁酱吖の 提交于 2019-11-30 22:33:36
Does anyone know why I am getting this error: The model item passed into the dictionary is of type system.Data.Entity.DynamicProxies.Object_3E186F803589BF82895B10E08C 2A9AB68EFA619599418D6EB96585D14874CDC0', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MyApplication.Domain.Entities.Object]'. Here is my code: Controller: public ViewResult Index() { if (User.Identity.IsAuthenticated) { MembershipUser currentUser = Membership.GetUser(); Guid currentUserId = (Guid)currentUser.ProviderUserKey; if (currentUser != null && currentUser.ProviderUserKey !=