I am trying to jump from ASP Classic to asp.net. I have followed tutorials to get Entity Framework and LINQ to connect to my test database, but I am having difficulties figuring out ExecuteQuery(). I believe the problem is that I need an "entity class" for my database, but I can't figure out how to do it. Here is my simple code:
Dim db as New TestModel.TestEntity
Dim results AS IEnumerable(OF ???) = db.ExecuteQuery(Of ???)("Select * from Table1")
From the microsoft example site, they use an entity class called Customers, but I don't understand what that means.
Entity Framework comes with a visual designer. In that designer, you connect to your existing database, and you select all the tables (and possibly views) from your database that you want to work with.
From that selection, EF will generate entity classes one for each of your tables, e.g. if you have a Customers table, you'll get a Customer class, if you have a Products table, you get a Product class.
Those classes represent (by default) your table structure 1:1 - e.g. each column in your table gets translated into a property on the class.
Once you have that, you're no longer dealing with SQL statements and stuff like ExecuteQuery() - you leave that to EF to handle for you.
You just ask for what you need, e.g. all your customers from a given state:
var ohioCustomers = from c in dbContext.Customers
where c.State = "OH"
select c;
This statement will return an IEnumerable<Customer> - a list of customers that matches your search criteria.
Related
I have an asp.net MVC solution, Entity Framework code first, which has dozens of database tables all designed around a single company using the solution.
The requirement has come up to allow multiple companies to use the solution, so what we have done is add "CompanyID" as a column to all database tables and set a default value. There is a company table with the various company names and CompanyID's. On login the user selects the company they are logging in as which stores the CompanyID in the session.
At the moment every Entity Framework call now has to be updated to include the CompanyID, for example when selecting Employees I am doing:
List<Employee> employees = db.Employees.Where(x => x.CompanyID = Session.CompanyID).ToList();
As you can see it will be tedious to do this on thousands of calls to the db. Any update, save, and fetch has to change.
Surely I am doing it the long way and there is a way at runtime, globally to append all DB calls to include the CompanyID stored in the logged in users Session? Something that dynamically appends the CompanyID when fetching values or storing etc? Perhaps a package I can use to do this task at runtime?
In my opinion, there is no need to add CompanyID to EVERY table in the database. I would select just "root" tables/entities for that. For example, Employee or Department clearly sounds like a many-to-one relationship with a company - so adding CompanyID there sounds right. But, for example, EmployeeEquipment which is a many-to-one relationship with Employee does not have to have CompanyID column since it can be filtered by the joined Employee table.
Regarding your request to filter by CompanyID globally, I'm not aware of anything that can do that per request. There are global filters for Entity Framework, but I'm not sure how you can apply them per-request. Take a look on .HasQueryFilter() during model creation if you are using Entity Framework Core.
I'm working on symfony2 project using doctrine2, I have an issue about creating a new button for user connected to a form to add 10 new fields with 10 values of course and 10 columns in DB:
that means when a user click "add new" button so he can adding one field X with XV value and creating one colomun "X" too in database , then second field Y with YV... until 10.Also XV AND YV are the same type of field for example "decimal".
I don't have problem with adding a new field in form using ajax,but my questions are :
1- How to create a new column in a table in DB with doctrine2 and insert value too?
2- how to update Class Entity and Class form too automatically in the same time when a new column is created?
First of all I have to admit I did not understand the goal you are trying to achieve completely. But I am almost positive, that there is a conceptual mistake in your design.
You have to distinguish between the following two concepts:
DML (Data Manipulation Language), such as INSERT, UPDATE or DELETE data in/from the database, meaning that you create new records (INSERT).
DDL (Data Definition Language), such as CREATE TABLE (to create a new database relation) or ALTER TABLE (to add new columns to the database).
You are trying to use DDL (ALTER TABLE statements according to your description) to dynamically add new columns to your database, which is not effective and not good practice at all. You should have an extremely good reason to do it that way (e.g. if the system you are building is a database administration tool or something like this...).
I would heavily recommend you to completely rethink and redesign your architecture and use DML to achive your goal. I am sure your problem can be solved by designing a flexible data structure and not adding new columns, but inserting new records in a dynamic database.
After you have redesigned your database structure it will be very easy to implement your task with Symfony and Doctrine.
I am builing a ui query builder with Entity Framework. I was thinking of letting the user choose a base entity type from a drop down list of entities, then populate a grid view as test. If you select this entity, your basic result will be the contexts of this grid view. Later I will add the ability to 'Include' other entities and filter. But for now I can't even get the basics. I have a drop down of all entities. But I do not see how to get a generic list based on the name of the entity. I think I need to use reflection, but can't figure that out. I can get this easily if I hard code the entity type, but that does not solve my problem.
I have DbContext set up, using entity Framework 5, and want the results on a ASP .Net webforms page.
I have a table in another database I would like to scaffold via ASP.NET Dynamic Data and incorporate into my existing Entity Model - is there anyway to do this? (eg using a view or other mechanism or customize the view, edit or insert operations via ad-hoc SQL or stored procedures?)
I don't want to replicate the entire DynamicData sub-folder structure and create another entity model for just one table
I was able to solve this by manually creating an entity in the SSDL and CSDL sections of the .edmx file by using a DefiningQuery and then defining the EntitySets for my entity class
I also added insert / update / delete Function elements to the SSDL with inline SQL using the CommandText property
At this point I had enough to let the Designer map the CRUD methods to these inline SQL functions I defined
It's a little tricky but it works and the general approach opens up many possibilities I had not thought about
I'm trying to create a many-to-many relationship in Entity Framework (code first), according to the following post: Database design for limited number of choices in MVC and Entity Framework?
However, I can't get it to work properly, and I'm sure I'm doing something very simple the wrong way. Here's the diagram I have no from my attempts:
The point of the junction table is that I need to have an extra property, Level, in the relationship, so I can't just go with a direct relationship between Consultant and Program. I added the ConsultantProgramLink entity manually in the designer, and then added associations to Program and Consultant respectively, selecting to add a FK for each, and then made them both primary keys. But when I do it like this it doesn't work as I expected:
If I had done a direct association between Consultant and Program, I would have been able to refer to, say, Consultant.Programs in my code. But that doesn't work now with the junction table. Is there any way to remedy this, or do I always have to go through the junction property (Consultant.ConsultantProgramLink.Programs)? In any case, even if I do try to go through the junction property it doesn't help. I can do Consultant.ConsultantProgramLink in my code, but another dot doesn't give me the navigation property Programs (which for some reason also became simply Program, why? Can I just rename them if I eventually get access to them at all?).
So what am I doing wrong? Why can't I access the properties through dot notation in my code?
Once you model a junction table as an entity you indeed lose direct many-to-many relation between Consultant and Program. That is how it works. You will either have direct many-to-many relation or additional properties in the junction table. Not both. If you want both you can try creating custom Programs property on Consultant and use linq query to get related programs:
public IEnumerable<Program> Programs
{
get
{
return this.ConsultantProgramLinks.Select(l => l.Program);
}
}
The example is also the explanation of your last problem. You can't have Program property on ConsultantProgramLink because it is a collection of related entities, not single entity (it should be called ConsultantProgramLinks). The property in ConsultantProgramLink entity is called simply Programbecause it represents single entity not collection.
Edit:
If you need each Program to be automatically associated with each Consultant you must enforce it when you are going to create new Program. Having junction table exposed as separate entity will probably allow you achieving it easily:
var program = new Program();
...
context.Programs.AddObject(program);
var ids = from c in context.Consultants
select c.Id;
foreach (var id in ids)
{
var link = new ConsultantProgramLink
{
ConsultantId = id,
Program = program
};
context.ConsultantProgramLinks.AddObject(link);
}
context.SaveChanges();
If you add new Consultant you will have to create links to all programs in the same way.
The disadvantage is that if you have for example 1000 consultants this construct will create 1001 database inserts where each insert will be executed in separate roundtrip to the database. To avoid it the only option is either use stored procedur or trigger on Program table.