C# - Loading A Nested Repeater With Data From 2 Entity Framework Queries?
Jan 20, 2010
I have 2 tables A and B that have a many to many relationship. I am using nested repeaters to show the data in a web page. My issue is, how do I write an ObjectQuery or an IQueryable query that returns the parent rows in A and the child rows in B so that I can use them as my data sources for my inner and out repeater. I have the code I wrote so far below but I am not sure if it is correct or even close.
I'm coding a business layer for an ASP.NET application. I've created database methods in my BLL as static. I've created public static Func variables to be compiled and used in several different methods, like this:
namespace BLL public class User { public static Func<Context, variable, result> selectUser; private static void CompileQuery() { if(selectUser == null) { selectUser = CompiledQuery.Compile...... } } public static UserClass Select(int id) { CompileQuery(); //uses selectUser } public static SomethingElse DoSomethingElse() { CompileQuery(); //also uses selectUser } }
It'll be used in ASP.NET layer like this: using BLL;
private void AddUser() { UserClass user = User.Select(id); }
My question is, since static variables are not thread-safe, is this a bad design decision? I'm thinking of either implementing a locking mechanism, which makes me think if it'd slow down the application, or using instantiated class approach which makes me wonder if query compiling would be beneficial.
Problem is, I also want to show the viewer the available number of spots in the room. But this requires somehow pull in either the current_occupancy / max_occupancy or in performing a calculation (e.g. max_occupancy - current_occupancy = actual_available) and then returning that with the room.
The end result I'm looking for is to return each room in a button control with text that looks like this: "Room 1 - 2 Open" "Room 8 - 1 Open" and so on
I need to dynamically access some SQL tables hopefully using the Entity Framework. Here's some pseudo code:
var Account = DB.Accounts.SingleOrDefault(x => x.ID == 12345);
which will return me an Account object and this contains some fields called "PREFIX", "CAMPAIGN ID" and further information about the accounts are stored in separate SQL tables with the naming convention of PREFIX_CAMPAIGNID_MAIN.
The tables all have the same fields so I was thinking of creating a new Entity that isn't mapped anywhere and then dynamically loading it, like so:
var STA01_MAIN = new MyAccount(); // my "un-mapped" entity DB.LoadTable('STA01_MAIN').LoadInto(STA01_MAIN);
I can now get anything about the STA01_MAIN account: STA01_MAIN.AccountId. So my question is: how do I access these tables using the Entity Framework?
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 }
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 am trying to add a new entity and have to refernce associated data to add it. I cannot load the Referencetables. Giving "The EntityReference could not be loaded because it is not attached to an ObjectContext." How do i complete this task in Entity Framework 3.5
I am using a repeater to build a custom table. However, I can't figure out how to make the table show subtotal if the next rows tour does not match the previous rows.
Something similar to that.
row1 tour1 row2 tour 1 tour1 subtotal row3 tour2 row4 tour2 subtotal total
[code].....
but this shows all the data in the nested repeater not the ones that match the parentrepeater fields for example tour,theme,dep7 should match in the child repeater
what changed do I need to make to my code for it to achieve what I'm after.
At the moment I am getting a "cannot cast to type" error message with the below code.
I have also tried calling the ItemDataBound method in with the parent repeater tags and had no errors but when I clicked on the button it would just move back to the top of the page and would not hide or show any data. Also I have made the div style to none but the first record still shows its child but the rest don't.
i want to display results from multiple queries in a repeater... is it possible? for example, a user searches for a keyword "test" i search 4 different databases for it, and display results from all 4 in one repeater.
I have a web app for our golf club. When I compute handicap index for each golfer, I have to select the most recent scores and then a subset of those scores depending on how many rounds of golf the golfer has played. All the scores are entered into a single SQL Express table called "Rounds". Verbally, this is what I'm trying to do:
1) select the twenty most recent golf scores (sort on date descending, "take(20)") [if less than 20 records, then select all available];
2) for this set of records, select the 10 lowest scores (or smaller number if golfer has less than 20 rounds);
3) compute the average round differential for the subset of records, etc. to calculate handicap index (this step is working ok...)
My current VB code has this LINQ query (which is flawed -- it selects the lowest handicap differential scores of ALL records for the filtered user):
[Code]....
How do I modify this query to accomplish items 1) & 2) above? It seems this should be simple, but my experience with queries is still limited.
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 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?
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>(); }................................
I am mapping a stored procedure to an entity by right clicking on the entity (in the .edmx) and selecting "Stored Procedure Mapping." This brings you to a Mapping Details - "Name of Entity" Window that allows you to select the insert, update, and delete stored procedures associated with the Entity. It also maps the stored procedure parameter to the Entity "Property" (Column).
I'm gettin an error "error 2042: Parameter Mapping specified is not valid." The cause of the error is fairly obvious, in the Insert stored procedure that has been selected, a 'CHAR' parameter is being mapped to an Int32 Entity Property. I altered the stored procedure parameter to match the entity, I deleted the stored procedure, readded, and reslected it as the Insert function. I also cleaned, validated, updated model from database. No matter what I do, the parameter list in the mapping details doesn't reflect the change to the stored procedure. It's stuck on a char --> int32 mapping, even though it has been changed, like it's buried deep in meta data some where.
I have a stupid problem with the Entity Framework. I get the following Exception:
[Code]....
I have an entity with a 4 fields representing the primary key. I copy it via serialization (works fine). I set the old entity to not valid (Datefield set to a date in the past, this field is part of the PK) and set the copied entity to DateTime.Now.Date. When I call context.AddObject I get the Exception above. I tried copying the entity via reflection but the entity has 3-4 references. And when copied, I get another Exception before even Adding the entity to the context. I also tried setting newObj.EntityKey = null and reset all the fields neccessary for the PK. I just want a whole copy of an entity with a different primary key (and some other fields changed too) and Create it in the database.
I am using a repeater control and i want to use one more repeater control inside the existing repeater control .
Like this:
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> </HeaderTemplate> <ItemTemplate> <!-- start child repeater --> Here I want to use one repater control <!-- end child repeater --> </ItemTemplate> </asp:Repeater>
I have been asking my self this question from the time i started workin in the software development field, What is the best way to access data? Which gives the best performance? Which is the best for maintainability? There are lots of solutions to deal with database in the Asp.Net web app,Entity framework 4.0 Classes generator using ADO.Net such as Code Author ( i liked the way it works and the way it accesses the database using the data access block in Microsoft Enterprise Library). i will start a new project tomorrow,and i don't know which approach is better?
I'm writing a custom .NET MembershipProvider (not the built in one) and trying to update using Entity Framework. But of course i have no access to (Try)UpdateModel. How can i update it?
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:
Here's a simple code snippet where I create a new Entity object and call SaveChanges() to create a new record in the mapped database table.
[Code]....
What I'm trying to figure out is whether there is a way to derive this new key for my entity, e, from the ObjectContext, m? The reason for this is that the way my actual code is structured, my entity object is already out of scope at the point where I call the SaveChanges() method.
But ofcourse it is not working as I want to. The Ordering works on Question.Order, but I would also the Questions.SubQuestions List to be ordered according to SubQuestions.Order