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.
Related
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.
I need to recreate a database with exactly the same values it has been originally created. So I need to add records with a pre-defined PK value. In this case, the PK is Identity in the database and when I try to define it's value, it is simply ignored, getting its value from the identity. No error is raised but the PK value that I supply is ignored.
example:
Category category = new Category()
{
CategoryID=1,
CategoryName="Beverages",
Description="Soft drinks, coffees, teas, beers, and ales"
};
ctx.Categories.Add(category);
ctx.SaveChanges();
Notes:
I'm using POCO, code first, so, I don´t have an EDMX Model to configure.
I don´t want to use ctx.Database.ExecuteSqlCommand(). I wish to maintain an Database agnostic approach.
In this case, the PK is Identity
In such case you should never manually insert its value. Once you set column as identity DB should be responsible for controlling the Id. Because of that there is no way to pass the value from EF (unless you want to break other functionality). You must use ExecuteSqlCommand and create complex SQL which will:
Turn on identity insert for the table
Insert record
Turn off identity insert for the table
Inserting value into identity column must be allowed by SET IDENTITY_INSERT tableName ON
I don't know if you scenario will let you do this, but if you define a composite key like as follows:
modelBuilder.Entity<Category>().HasKey(s => new { s.CategoryID, s.Name });
(using HasKey while running the DbContext.OnModelCreating method and EF 4.1 Code First), then you actually can control which values get inserted when you save the POCO object to the database.
I will say that, however, I would agree with Ladislav insofar as that the primary key values you are trying to maintain here are conceptually really more like data than record identifiers, and should be treated as such. Meaning, treat them as just data fields, and create a new primary key field on your POCO class in order to uniquely identify database records. e.g. for Category
public Int32 PK {get; set;}
and be sure to indicate it's intended to be the PK field from OnModelCreating
modelBuilder.Entity<Category>().HasKey(c => c.PK)
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.
If I have a table with two foreign key fields to another table, I.E.
Table: User
Field: FK_PrimaryItem_ID
Field: FK_SecondaryItem_ID
Table: Item
Field: ItemID
When I'm using the entity framework, the generated objects become:
User.Item
and
User.Item1
and I can't differentiate between the two of them. I can map back to the name of the foreign key, but this is a difficult way to go about it. How can I find out which one, Item1 or Item is which field?
I would like to leave my EDMX file auto generating if possible.
I've not found any problems with updating my model once I'd changed the name of the Navigation Properties on the design surface.
In general, User.Item would be represent the first column the model came to with that foreign key, and User.Item1 would represent the second column.
But as I said, I just went into the model, and changed the name of the Navigation Properties to more usable names based on the association listed in the Mapping Details.
I had the same problem with a self-referencing key:
PageID
Parent_PageID (refers to PageID)
Until I renamed the Navigation Properties to "Parent" and "Children" respectively. The toughest part was figuring out which is which, which I did by noting the Multiplicity property on the NavigationProperty objects (0..1 for parent, * for children)
I'm trying to wire-up LinqToSql to be my data access layer and running into a few issues, probably due to my lack of experience with LinqToSql.
I have two tables, one called Project and one called Employee. The Project has fields for OpenedBy and ClosedBy which are foreign key references to the Employee table, which has fields for EmployeeId and Name.
When I fetch a Project I would like for it to fetch the EmployeeName for the OpenedBy and ClosedBy. I would like to access these like the following:
// assuming data is of type project
this.OpenedByName.Text = data.OpenedByName;
this.ClosedByName.Text = data.ClosedByName;
Is it also possible to set these values whenever OpenedBy or ClosedBy changes? Is this possible? Sample code would be much appreciated!
Clarification
I would like to do this without having to use stored procedures.
If you have 2 relationships coming from the Employee table, I think you'll have 2 child properties, project.Employee, and project.Employee1 in each Project entity.
You can change the name of the association, just go to the relationship properties, select Child Property and there change the name of each child Employee to be more descriptive.
You can name the child properties as you want, for example you could:
this.OpenedByName.Text = data.OpenedByEmployee.Name;
this.ClosedByName.Text = data.ClosedByEmployee.Name;