I have following tables
Cart : CartId, ProductId,UserId,PromoCodeId
Promocode : Id, Name , Discount
(Promocodeid is not a foreign key)
I want to get name of promocode from PromoCodeId in MVC using EF, i can not set foreign key because I also want to allow null values in PromoCodeId of cart table. how to achieve it?
Related
i am new to oracle 10g. I am using it to build some forms. I have created all the tables in sql+ and i have designed my forms. I am having a problem with the foreign key fields in the form.
I have a table named class, class has 4 foreign keys which are represented on the class form
R_ID which is the primary key of a table called ROOM
E_ID which the primary key of a table called EXAM
T_ID which is the primary key of a table called TERM
SUB_ID which is the primary key of a table called SUBJECT
In the class form i want the user to be able to create or update a record of a class. For user friendliness i want when they put the cursor on any one of the foreign key field to enter its value, I want the corresponding NAME from the parent table to be displayed as a drop down list instead/as well as the numerical foreign key value. The user's choice would populate the foreign key field of the class table so
IF R_ID = RM001 and the name of that room in the parent table is Chemistry LAB. I want the user to see chemistry lab and choose that but it populates the class table in the database as RM001
I have created all my navigation manually via buttons, this is the last thing i have to do and im stumped. I want the user to be able to click the field and the drop down list automatically appears.
Any ideas? thanks very much
Found the answer for people you are seeking this information
Use the list of values (lov) wizard.
Build query in the wizard by selecting the attributes of the table you want( the parent table of the foreign key)
Set the return value as the foreign key in the child table
Assign a button to execute the lov created
This works
Someone else asked a similar question here: How can I use EF6 to update a many to many table
I mention that up front because I couldn't get any of the solutions given to work.
I also studied the solution give on Code Project: http://www.codeproject.com/Tips/893609/CRUD-Many-to-Many-Entity-Framework, but this doesn't work for me either.
I'm trying to keep this as simple as possible.
I have two tables: dbo.Teacher and dbo.Student. Each has an "ID" column that servers as a primary key. I also have a third table called dbo.StudentTeacher which has exactly two columns, both are non-nullable and foreign keyed to the previous two tables; in other words, it establishes a many-to-many relationship between teachers and students. As expected, the EDMX designed shows only dbo.Student and dbo.Teacher and infers the relationship between them.
Here is a script for the above; there is nothing else in the database.
CREATE TABLE dbo.Teacher
(
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR(MAX)
);
CREATE TABLE dbo.Student
(
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR(MAX)
);
CREATE TABLE dbo.TeacherStudent
(
TeacherId INT NOT NULL FOREIGN KEY REFERENCES Teacher(Id),
StudentId INT NOT NULL FOREIGN KEY REFERENCES Student(Id)
);
INSERT INTO Teacher(Id, Name)
VALUES
(101, 'Tom');
INSERT INTO Student(Id, Name)
VALUES
(201, 'Sue'),
(202, 'Stan');
INSERT INTO TeacherStudent(TeacherId, StudentId)
VALUES
(101, 201);
Now that I've established my data structures, I want to accomplish a very simple task. From the script above, you can see that we have one teacher named "Tom" who has a student named "Sue". We also have a student named "Stan" with no teacher. My task is to modify the database so that Sue is no longer Tom's student and Stan becomes Tom's student.
To accomplish this, I wrote the following code:
class Program
{
static void Main(string[] args)
{
using (var entities = new TestEntities())
{
// There is only one teacher in the system.
Teacher teacher = entities.Teachers.Single();
// This teacher has a student #201: Sue.
// I want to replace her with student #202: Stan.
teacher.Students.Clear();
teacher.Students.Add(new Student() { Id = 202 });
entities.SaveChanges();
}
}
}
It looks very simple: clear the students associated with Tom and then add Stan as Tom's student. However, when I run the code, I get the following error: Unable to update the EntitySet 'TeacherStudent' because it has a DefiningQuery and no <DeleteFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
I tried simplifying the problem by trying to just remove Sue from being Tom's student without adding Stan, and I get the exact same error message.
As I understand, this error normally occurs when Entity Framework doesn't have enough information to do what you want it to do, but I really can't see what's missing. There are two simple tables with a join table between them and I need to be able to change which rows are related to which other rows.
I should also note that if I'm not mistaken, the change that I wish to make in this example should affect only the dbo.TeacherStudent table; the other two tables should not be touched.
Okay, after some more Google-Fu, I figured it out.
Even tho the join table must have only two columns with each column foreign keyed to the two tables to be related, the join table still needs a primary key, which can be a composite of the two foreign keys.
Thus, dbo.TeacherStudent should be created with this:
CREATE TABLE dbo.TeacherStudent
(
TeacherId INT NOT NULL FOREIGN KEY REFERENCES Teacher(Id),
StudentId INT NOT NULL FOREIGN KEY REFERENCES Student(Id),
PRIMARY KEY(TeacherId, StudentId)
);
I'm having a hard time finding the exact answer to this question, so my apologies if this is redundant.
So I have 3 tables defined such that:
Person :PersonId, FirstName, LastName
Company: CompanyId, CompanyName
Order: OrderId, PersonId, CompanyId
On the Order table, there is a foreign key defined on the PersonId and CompanyId columns, thus, my Order entity class generated by EF has a navigation properties of type Person (not PersonId) and Company.
So, to insert into the Order table, I first need to query the person and company tables to get the person and company entities. Then I can construct the Order object using the Person and Company entities and save it to the db.
In my scenario, I am being passed a PersonId and CompanyId.
In classic SQL I would just do INSERT INTO Order Set (CompanyId, PersonId) - 1 database call. But with EF, I have to do 3 db calls. This seems like overkill.
Is there any way around this?
PS - I'm using EF 6. I know I could generate an expression and make it single call..but that would still yield two subselects.
You can just include foreign key properties in addition to the navigation properties and then set them using the ids you have. If you do this will not have to go to the database to get related entities for just a sake of setting the relationship.
This is a simplified version of the problem I'm facing. Basically, I have an existing database with code-first Entity Framework on top of, and there are several tables where I see an ObjectId and ObjectType field. Depending on the ObjectType, the table can be joined to one of several other disparate tables which do not share a hierarchy.
For example, given the following database tables:
CREATE TABLE BarCode
(
Id int NOT NULL PRIMARY KEY,
ObjectType int NOT NULL,
ObjectId int NOT NULL,
BarCodeValue varchar(20) NOT NULL,
)
CREATE TABLE Person
(
Id int NOT NULL PRIMARY KEY,
Name varchar(20),
)
CREATE TABLE Package
(
Id int NOT NULL PRIMARY KEY,
Name varchar(20),
)
I might see rows in the BarCode table with an ObjectType of 1, which correspond to Person records, or rows with an ObjectType of 2, which correspond to Package records.
In Entity Framework, how do I set up a mapping so that the Person entity can have a BarCode and the Package entity can have a Barcode? I don't see where the Map() method allows you to specify any fields other than the foreign key field. Like I said, there's really no hierarchy among these objects, it's more that they share a common attribute. Sort of like decorating a class with an interface vs. having it inherit from something.
I have my Entity model where every entity has a primary key and they are marked with StoreGeneratedPattern = "Identity", the entity I am interested in is PEOPLE. It was created in oracle, and in another schema exists a "PEOLPE_HIST" table with it sequence. Before insert into PEOPLE I search on PEOPLE_HIST and if the person exists. I get the history information to including the ID (primary key) to insert into new PEOPLE table. I created a trigger for PEOPLE table to check if the ID is greater than or equal to 0, if it is, then I will not use the sequence and insert the Hist ID.
Is this possible?
thanks in advance