EntityDataModel and Association table - c#

I have an authors -> authorsbooks <- books in my database. I created an entity data model from this and I noticed that the association entity didn't show up in the model(I think it's inferred).
I want to drag 2 EntityDataSources onto the designer, a gridview of authors, and when a user clicks to select an author another gridview below will show all the books assoiciated with that author.
How do I configure that second entitydatasource because when I try to configure it I don't see the authorsbooks association entity in the entitysetname dropdownlist. I'm thinking to set this as the datasource to the books gridview? Am I misunderstanding something?
Thanks,
rodchar

Entity Framework will infer the relationships based on the Foreign Keys in your database.
Does your AuthorBooks table have a foreign key to the Author table?
Also, when you add your tables onto your EDMX, make sure you tick "Include Foreign Keys in Model".
If done correctly, when you type Author., you should see AuthorBooks as an EntitySet.

Related

EF Foreign Key Associations Deleted

The picture show relation with two tables in my database;
Well as you can see there's a field called "DeviceTypeID" in the right side table.
Normally EF adds CompanyTypeID column to the ServiceLaburDefinitions model but it's disappeared last time I updated model from the database.
I am searching for a solution for a couple of hours but not able to find any solution. Could anyne suggest a solution?
Thanks.
ServiceLaburDefinitions is the depend entity and has the DeviceTypeID foreign key property defined.
So Entity Framework creates a navigation Key under the hood between the 2 tables based on DeviceTypeId key.
You can see for example how a navigation key is created also in the following example between the foreign key and the primary key
More information about navigation properties can be found here
When you create the model from the database, there is a checkbox marked "Include Foreign Key columns In The Model" - Make sure this is checked.

Entity Framework with many to many relationship generetad tables

Here's my question. I have 2 models (Person, Event) and with EF and modelbuilder I generate a booking table (with IdPerson and IdEvent as properties).
So in my DB it's correct, I have 3 tables (Person, Event and Booking) with many to many relationship. But I have only 2 models in Visual Studio (Booking doesn't exist because of the self-generated table).
With my Controller I want to write an action for the Person to suscribe to an event and I have to write on my table Booking on the DB but it doesn't exist as a model so I can't do that .
How should I proceede?
Should I create a Booking model and delete my modelbuilder?
When you are using ORMs like EF, you can sit back and let the ORM manage these middle tables.
You can use
person.Events.Add(event)
or
event.People.Add(event)
and EF handles all and inserts a row with personId and eventId in that table.
Here you can find a complete sample:
http://blogs.msdn.com/b/wriju/archive/2011/05/14/code-first-ef-4-1-building-many-to-many-relationship.aspx
I assume this is a model first approach.
The reason for having only 2 objects is that, by default, EF does not create objects for joint tables. What it does create is Navigation Property (Entity Framework - Navigation Property Basics). In one-to-many scenario, a navigation property inside a parent object contains a collection of entities in a foreign / child table. In many-to-many scenario, navigation properties of each entities will simply contain collections of its other entities.

create one to one REFERENCES in EF designer?

I have two tables employees and aspnetUser. I'm trying to create an Associate between tables employees and aspnetUser in EF designer in VS2013 using email from employees and username(email).I createUNIQUE Constraint on employees email to have UNIQUE email for each employee I have Problem when click ok I have this error message "verify that the navigation property name is unique" I have couple of Problems.
1-I try to use ID from asp.net membership but it is varchar(128) not guid because of that I have to change my employees ID from int to varchar(128) to very table have relation with employees.
2- how to solve this problem apically I don't have access to database because the easy thing is to create REFERENCES CONSTRAINT between users and employees I really don't have access to database because of company ?
The error tells you that you're introducing new navigation properties with names that already exist in the types you're trying to associate. In the text boxes below the Navigation Property checkbox you're not selecting properties. You're entering names for the new ones you've indicated you want to create by checking the checkboxes.
This is not going to work. If the types of the primary keys are different, EF won't be able to create a "soft" association between them (where a "soft" association is one that is not backed by a hard foreign key in the database).
You only option is to manually join the two tables whenever you need data from both.

How to get parent entity key?

I use EntityFramework and in my edmx file I have two tables mapped:
Customers
column:
Id PK
and Orders
columns:
Id PK
CustomerId FK
which are associated.
When I take Order object there IS Customer property but I can't see CustomerId property.
I used to work with L2SQL and I expected to see CustomerId but EF somehow hides it.
the Add Association dialog in the EF edmx designer allows you to specify (via a checkbox) whether or not you want to have a "Foreign Key" (and/or a Navigation Property) created for the association. did you make sure it's checked?
Inside the Customer property, there should be a CustomerId property.
So try..
myOrderObject.Customer.CustomerId
How exactly is the id column in the order table named? Usually the EF-Designer should create a property for each column in the database table. Have a look at the "Mapping details" page (which you can open in the context menu of the EF designer) to see if (and to which property) the column is mapped.

Problems with automatic property creation when manually mapping relationships between tables with no foreign keys in L2S

Pardon the massive headline.
I'm in the situation of having to build an application on top of a database, that I cannot make any changes to. The database does not have any primary- or foreignkeys set.
I'm using linq-2-sql, and I'm interested in having some properties exposed on the entities generated from my dbml. For instance, in the hypothetical example of a one-to-many relationship between table education and student - where each student record has a reference to an education id, I'd like to be able to go:
var student = GetAStudentFromContextOrWhatever();
var studentsEducation = student.Education;
It is my experience, that this kind of property is automatically generated when I drag'n'drop tables with foreignkey relationships from the server explorer.
However as previously mentioned, in this case I do not have these foreign key relationships - rather I am adding the relationships manually in the dbml file, specifying parent and child class.
When I add these relationships, I expect the involved entities in the designer.cs of my context to get populated with properties of a kind like those described above.
This, however, does not happen.
What must I do for my dbml to create these properties for me - based on these manually mapped associations between entities/tables that, on a database level, do not have foreign key associations?
Cheers!
L2S is just that Linq-to-SQL. If it isn't in SQL it won't be generated. The expression trees behind L2S just can't understand what you are doing. The place for your association is in a partial class file which you will have create manually. Also it probably won't update or insert through the association.
I know this is a very old question, but I just ran into the same problem. In order for the relationship in the DBML designer to automatically create the association properties for you, you need to have primary keys on your objects. If you click the column name in the designer, you'll see that your PK field has PrimaryKey = false. Switch that to True and build; all should be well.
Patrick

Categories

Resources