I'm setting up a project with Doctrine 2.1.5. I have got few tables without primary keys. I added some primary keys temporarily to generate Entities and Repositories.
But I will delete the fake primary key columns and remove the reference from Doctrine Entity as well.
Also I will be defining few columns as composite primary key in those table Entities (but not in tables).
Will there be any consequences (eg: wrong result from query or failing query) when joining those entity classes Because of 1. not having PK in database 2.adding fake composite primary key in entity classes?
Lots of tutorial and blogs mentions that needs to have primary key. But, never anything mentioned (which I can find) that the above solution is not a problem. (or it is a problem).
An answer from real experience would be great. But everyone welcome.
Thanks in advance for help.
P
The .NET Entity framework is giving me the following error:
"The table/view 'Foo.dbo.vwFoo' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity you will need to review your schema, add the correct keys and uncomment it."
The view is a collection of a variety of tables and calculations. What I'd like to do is create a "logical key" using one of the columns that I know should be unique. I can't figure out how to do this in SQL Server Manager 2005 (not a DBA.)
Anyone know how I could accomplish this?
The EF can't find the PK, because VIEWs don't have PKs. However, if the view returns a unique column, you can tell the EF that this is the "PK."
It's explained in this tip: How to work with Updatable Views
I am trying to create a mapping to a database table that has no primary keys/references.
public class TestMap : ClassMap<<Test>Test> {
public TestMap() {
WithTable("TestTable");
Map(x => x.TestColumn);
}
}
This fails and expects id or composite-id. Is this possible in fluent nhibernate?
In Oracle at least, I have used "ROWID" for this. For mssql you might use the "ROW_NUMBER()" builtin function for readonly access to the table, but I haven't tried that...
No. You'll have to add a surrogate primary key, such as an identity column in SQL Server, to map this table. As far as I know, this isn't supported by NHibernate itself.
Why don't you have a primary key on this table?
This functionality isn't supported by nhibernate as far as I know. As a general rule of thumb, however, you should really always have some kind of ID and if you find yourself in a situation where you think you don't need one you should assess your data model. An ID, whether it be a table-specific primary key, or a surrogate key from another table, should exist. This not only ensures that nhibernate can process the table, but helps performance via indexing.
Before you start assuming nhibernate isn't going to fulfill your needs, consider why you don't have a key on the table and what kind of sense it makes not to have one.
If we can bring a column from table having no primary key/identity coulmn, then we can use fluent as below:
Id(x => x.TempID).Column("TempID");
If the table contains data that belongs to another entity, you could map it as a collection of components. Components are not identified by themselves, but they belong to another entity, which is identified.
You can map an entity to a table without keys defined in the database. I do so in legacy SQL Server databases. However, the table must have a candidate key (some set of columns that actually stores a unique combination of values). The concept of entity involves the notion of some kind of identity.
Instead of this, what you're trying in your code is to map an entity without identity, wich isn't possible.
Is it beneficial to use multicolumn (composite) primary keys for a many to many relationship table when using Linq to SQL?
Or should I just add an identity column as a non-clustered primary key and index the FK columns appropriately?
Not a LINQ issue. If you need them for your schema, then use them. If you don't, don't. Either way, LINQ will handle your schema just fine.
One area that LINQ to SQL doesn't handle well are multy column / key mapping table that are used to connect a many to many relationship but I wouldn't say this strickly falls under the category that your question addresses. You can still perform CRUD operations on a mapping table within LINQ but LINQ cannot walk the relationship presented by a many to many mapping table. (LINQ works fine with one to one and one to many tables.)
I can't speak to any issue with the Entity Framework but again, I would be very surprised if the EF had any issues with multi-column / multi-key tables.
If it makes sense in your domain to have a multi-column composite key, then use one. Otherwise use the usual identity column as the surrogate primary key.
EDIT: that was general advice and not taking into account any technical aspects of implementing using LINQtoSQL. These may be of interest:
How to: Handle Composite Keys in Queries (LINQ to SQL)
LINQ To SQL Samples
Linq to SQL DTOs and composite objects
I need to use tables from a DB which I cannot alter (using linked server).
So part of my schema is a view on these table and I cannot create an FK in my DB.
When I come to creating the association in ADO.NET Entity Framework I am getting problems because a second column on the table from the external DB has an index on it and the EF is creating an Entity Key for it (it's the name descr of the record - I think they just wanted to speed the ordering on it).
When I take the Entity Key off this column in the EF entity it complains that I need it because the underlying table has a key on it.
If I leave it in I cannot map it onto anything in the table mapping of EF.
Does anyone know what I should do please?
You will have to edit the XML and remove the column from the key. Find the <EntityType> tag in the <edmx:StorageModels> section (SSDL content). Delete any <PropertyRef> in the <Key> that is not actually part of the primary key.
Once you do this, you can set "Entity Key" on the corresponding scalar property in the designer to false, and EF won't get mad. You will also not be asked to map this column in associations anymore.