But it creates the relationship (and the third mapping table) with cascade delete switched off by default. How can I tell EF to create the relationship with cascade delete switched on when using many-to-many?
I been looking but I cant find any info on this topic Anyway I got a model that looks like this.
[Code]....
I want to cascade delete a category with all its sub categories but I cant make that reference in the database, not with the option to cascade update or delete. It work fine with pagecontents and all the other I got its just this same table reference. In NHibernate I do the cascade setting hbm or with a better way using the fluent option. How do I go about this task now?
Im a fan of the EF code first and with its last preview of the CTP5 I wonder if it would be safe for me to use this for a smaller site for customer? I would love to get your opinions on this? And any good sources for tutorials and information would be sweet. I'm currently reading the post on scottgu's blog about it.
public class Category { public int CategoryId { get; set; } public string Name { get; set; } } public partial class CategoryMap : EntityTypeConfiguration<Category> { public CategoryMap() { this.HasKey(c => c.CategoryId); this.Property(c => c.Name).IsRequired().HasMaxLength(400); } } public class MyObjectContext : DbContext { public MyObjectContext(string connectionStringName) : base(connectionStringName) { } public DbSet<Category> Categories { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new CategoryMap()); base.OnModelCreating(modelBuilder); } }
Now when I run the following code I get an exception (GenericArguments[0], 'System.Int32', on 'System.Data.Entity.Internal.Linq.ReplacementDbQueryWrapper`1[TEntity]' violates the constraint of type 'TEntity')
DbDatabase.SetInitializer<MyObjectContext>(new DropCreateDatabaseIfModelChanges<MyObjectContext>()); using (var context = new MyObjectContext("NopSqlConnection")) { var query1 = from c in context.Categories select c.CategoryId; var test1 = query1.ToList(); //works fine var query2 = from c in context.Categories where query1.Contains(c.CategoryId) orderby c.Name descending select c; var test2 = query2.ToList(); //throws the exception }
I have a problem with strange generate sql in ef4 ctp5. I have simple model with mapping :
[Table("post_auction")] public class PostAuction { [Key,Column(Name="Id"),DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGenerationOption.Identity)] public int Id { get; set; } [Column(Name = "Number")] public int Number { get; set; } [Column(Name = "Label")] public string Label { get; set; } [Column(Name = "Description")] public string Description { get; set; } [Column(Name = "CategoryId")] public int PostAuctionCategoryId { get; set; } [Column(Name = "PriceCZK")] public int PriceCZK { get; set; } [NotMapped] public bool IsAuctionPhotoExitst { get { if (File.Exists(HttpContext.Current.Server.MapPath("~/Public/Images/Posts/Thumbs/small_" + this.Number + ".jpg"))) return true; return false; } } }
I have a one-to-one relationship on two tables with are represented by 2 classes in my EDM. I am trying to save a new record and am confused how to save to the second entity. I have tried the following.
Dim myObject as new MyObject myObject.prop1 = 5 myObject.prop2 = "Test" myObject.myOtherObject.prop1 = 3 myObject.myOtherObject.prop2 = "Hello"
But I get an error when I hit myObject.myOtherObject.prop1 = 3 because myOtherObject hasn't been initialized. How do I initialize an child object under myObject?
I have 3 tables User, role and UserInRole THe entity frame work made entities for User and Role and I have successfully made a join with the user and role entities using user.add(role); but I can't seem to get it to remove the link (I want to retain the user and role just remove their association).
using (ProviderEntities context = new ProviderEntities()) { User u = context.User .Where(n => n.ApplicationName == application) .Where(n => n.Username == username).First(); Roles r = context.Roles .Where(n => n.ApplicationName == application) .Where(n => n.RoleName == roleName) .First(); r.User.Remove(u); context.SaveChanges(); }
The oposite worked when I did r.User.Add(u); but it wont let me now remove it after it been created. Driving me mad as I can't see where I have gone wrong.
I have an Events table and an InstallmentPlans table. The relationship is 0..1 : an Event can have 0 or 1 Installment plans. If I want to remove the existing InstallmentPlan for an event, how do I do this? Setting it to null doesn't seem to work:
I get an cast exception when i am trying to insert an entity in Entity Framework (using code-first). From this code :
public virtual T Insert(T entity) { return Context.Set<T>().Add(entity); }
The cast exception is like "impossible to cast ...Collection'1(Entity) to type (Entity)" I can't figure out why. I am pretty sure ive done everything right. Post entity
public class Post { public long PostId { get; private set; } public DateTime date { get; set; } [Required] public string Subject { get; set; } public User User { get; set; } public Category Category { get; set; } [Required] public string Body { get; set; } public virtual ICollection<Tag> Tags { get; private set; } public Post() { Category = new Category(); if (Tags == null) Tags = new Collection<Tag>(); }................................
How can i make my own delete method to prevent that the data really gets deleted? i want to set a datetime field when it gets deleted insted of a normal delete. i read about overriding the submitchanges function, but i don't get it to work
I have a good friend who owns a print shop and he has asked me to create him a simple online shop where he can take print orders. His print shop location is kind of hidden, and business isn't going well at the moment. This is why he wants to move online. I said I would do it for him, but it will take a while before he has anything to look at. Where I am most stuck is the Model. The problem with this sorts of shops is that each Product, depending on its category would have different attributes. How ? Here is an example: [URL] A user would choose a product category, then he/she would get to the following: [URL] Now he/she would click the Order link to order a certain qunatity. On the next page, depending on what category they are in, they'll get a form where they can choose order attributes. There aren't many attribues for BUSINESS CARDS. But if they had chosen CARBONLESS FORMS, they would get options to tweak like: Squencial Numbering to start from 103490Position of Numbering..etc
I asked for advice at the SQL Server forums, and finally I came up with something like this:[URL] But with that database design, I will have a lot of type casting to do for the attributes ! << In fact I'm not sure if that would be even possible. Then I watched this video, and the author does something cool ! she uses Inheritance. Then I started Wonding ... Is it possible for me to have a Parent [ Product ] class, and have its children entities with different (added) attributes?
ive tried to build some base project with above technologies. I wanted maximum flexibility and testability so i tried to use patterns along the way to make this as a base for future projects. However, it seem.something is wrong or whatever and i really need help here. So
i have two questions :1- Is there anything wrong with my current code ? Ive applied patterns correctly ?
2- Why do this code actually connect to the database, create it, but doesnt support insert even if i perform the corrects operation ? (Look at the end of the post for details about this error)
I have two entities : Comment and Review
COMMENT [Code]....
REVIEW[Code].... I built up a base repository for each of them this way :GENERIC REPOSITORY[Code]...For specific operations, i use an interface :[Code]....So i am getting the generics operations from the abstract class plus the specific operations :[Code]....As you figured out, i also use a database factory will produce the database context :DATABASE FACTORY [Code]....DISPOSABLE (Some extensions methods...)[Code]....DATABASE [Code]....And to finish, i have my unit of work....UNIT OF WORK[Code]....I also binded using Ninject the interfaces :NINJECT CONTROLLER FACTORY [Code]....however, when i call in the constructor ...[Code]....
This seem to create the database but doesnt't insert anything in the database in EF4. It seem that i may figured out the problem.. while looking at the database object.. the connection state is closed and server version throw an exception of this kind :
ServerVersion = '(((System.Data.Entity.DbContext (_database)).Database.Connection).ServerVersion' threw an exception of type 'System.InvalidOperationException'
I am doing the right things ? Is there anything wrong in what ive built ? Also if you have recommandation about the code i posted, i would be glad. I am just trying to the learn the right way for building any kind of application in MVC 3. I want a good a start.
I use : - Entity Framework 4 Code-First CTP 5 - ASP.NET MVC 3 - Ninject as DI Container
Is there a way to export a EF 4.0 Data Model to EF 3.5?
I looked around and found that we are not able to access EF 4.0 from a ASP.Net 3.5 project here: [URL]
Our project is the 1st to go to .Net 4.0 using Entity Framework and we (the team) were wondering if there was a way for the other projects that "might" need to access our data that are still using the .Net 3.5 framework.
i have a web-site which uses forms based authentication. thus i have a login page with a login control on it. how would i go about coding the authenticate event of this control to talk to the Entity Framework to validate the user credentials?
When I use classical ADO.NET or (which is interesting) EntityDataSource (e.g. for GridView), then page load within 1 second.But when I use ic codefile code like:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Cty = CType(Request.QueryString("cty"), Integer) ' course type Page.SetFocus(ddl_FGPhotosOK) If Not IsPostBack Then ' read course status Using ctx_sdbEntities As New sdbEntities() [code]...
You can find the source code demonstrating this issue @ http://code.google.com/p/contactsctp5/
I have three model objects. Contact,ContactInfo,ContactInfoType. Where a contact has many contactinfo's and each contactinfo is a contactinfotype. Fairly simple I guess. The problem I'm running into is when I go to edit the contact object. I pulled it from my contact repository. Then I run "UpdateModel(contact);" and it updates the object with all the values from my form. (monitoring with debug) When I save the changes though, I get the following error:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
It seems like after I call update model it nulls out my references and this seems to break everything?
Here are my models:
public partial class Contact { public Contact() { this.ContactInformation = new HashSet<ContactInformation>(); } public int ContactId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual ICollection<ContactInformation> ContactInformation { get; set; } } public partial class ContactInformation { public int ContactInformationId { get; set; } public int ContactId { get; set; } public int ContactInfoTypeId { get; set; } public string Information { get; set; } public virtual Contact Contact { get; set; } public virtual ContactInfoType ContactInfoType { get; set; } } public partial class ContactInfoType { public ContactInfoType() { this.ContactInformation = new HashSet<ContactInformation>(); } public int ContactInfoTypeId { get; set; } public string Type { get; set; } public virtual ICollection<ContactInformation> ContactInformation { get; set; } }
I am new to entity framework , it is really very good , but I want to know what is the difference between using entity framework with stored procedure or without stored procedure , which one the faster and what is the benefits for using stored procedures with entity framework.
I've been using EF as my primary ORM for quiet some time now. Today I decided to use EF Code First to create my model for my new project. So I went ahead and read a bunch of documents on MSDN and some blog posts by Scott Guthrie and others. Unfortunately, what I've read does not go beyond the basics of using Code First within an ASP.NET MVC application. But as you might know, in real-world applications the structure of the solution is a little different. Anyway, to make a long story short:
- I created all the POCO files that will be needed.
- I created the SharweEntities class which extends the DbContext class.
- I added the following connection string to my Web.config file
[Code]....
Then I started the application and checked whether a new database was created inside SQL Server 2008 Express, but found nothing. I'm not sure what's wrong but I suspect it might be the way I'm structuring my solution? Here's the structure:
- Sharwe.MVC : The ASP.NET MVC3 Web Application (has nothing except a HomeController and a corresponding simple view at the time being)
- Sharwe.Data : model class and data access logic (contains all the POCO classes and the SharweEntities class)
- Sharwe.Services : Business logic
Here's an image that shows the structure of my solution:
I have an EDM, it includes the entities extension and history. My goal is to use history to keep track of all the changes made to extension entity. For example, if extension with ID 223 has its property 'Name_Display' changed - I want the history entity to record this.
I'm using ASP.NET with VB.NET. Where in my code do I put the hook to say, "update the history entity" and what should that hook look like?