I have a table with a key and another field named "CardId" .
Now I want to make CardId unique. I mean just unique value should be store in CardId.
How can I do it?
thanks
Assuming you use SQL Server, you can simply add an unique index to your table, e.g.
CREATE UNIQUE NONCLUSTERED INDEX [IX_TableName] ON [dbo].[TableName]
(
[CardId] ASC
) ON [PRIMARY]
GO
This will ensure that the column only allows unique values to be stored.
As far as I'm aware, the only way to specify a column as "unique" in EF4 is to flag it as the Entity Key (please correct me if I'm wrong), but this could lead to some confusion down the line as your Entity Key should ideally map to the primary key of your table.
Related
I'm using database-first approach with Oracle. One of my table doesn't have a primary key. It only has 2 columns which are foreign keys of other tables.
I have generated model in ASP.NET MVC project from database (Add - New Item - ADO.NET Entity Data Model).
But there is a problem - I get an error:
Error 159: EntityType 'DbModel.Store.SomeTableWithoutPK' has no key defined. Define the key for this EntityType. E:\Git_repo\ZZ\ZZ.Domain\DAL\DbModel.edmx
Does this mean that each table must have a primary key? Can I avoid this? Or I will be forced to add new column with a primary key to this table? Of course there is also possibility to apply primary key to multiple column, but is it necessary?
Every table should have a primary key for database efficiency and so that you can edit records.
You don't need to create a new column for the primary key in your 2 column table
In designer, select both columns and use both together as the primary key. As long as nulls are not allowed and there are no duplicates you should be OK.
Since this is a many to many table and you are using EF, you may find later that adding a datetime column to the table with getdate() as the default value will make data maintenance easier
I have a table with a Clustered Columnstore index. This table cannot have a primary key because of the index. It actually has an ID column, but I cannot put an index on it.
Entity Framework demands a Key, otherwise it marks it as read only. How can I tell EF what the key is?
The answer is to add a key to the edmx. Open the EDMX, right click the column and select entity key
I have a table defined in a SQL Server database as follows
Columns
MID Int foreign Key
MRUOMID Int foreign Key
All columns in this table are foreign keys and referring to other entities.
I wish to add an Identity column to this table. If I do so and update the EDMX model from the database, I get the following error.
problem in mapping fragments starting at 7462. You must specify mapping for all key properties.
I need an identity column to be added which I can refer to in my Entity model further.
Please let me know on this issues.
Thanks.
While updating, make sure you include the foreign keys too.
I am currently using a Dbsetlist.Count() to get the primary key of an Autoincremented table where an Row will be saved before any actual saving is performed.. But I feel this approach is not correct but cannot figure out any other alternative purely using Linq can someone suggest how to do this in Linq only?
db.Invoicesets.Add(invoice); //Invoicesets is Invoice table as list for Linq
order.invno = db.Invoicesets.Count(); // Invno is needed as a foreign for Order table
db.Ordersets.Add(order); //
db.SaveChanges();
Do you mean, say, you want to add entity A first so you can use the identity column of A, A.ID as the FK to entity B? If yes, you can use InsertOnSubmit():
db.Invoicesets.InsertOnSubmit(invoice);
order.invo = invoice;
db.orders.InsertOnSubmit(order);
db.SubmitChanges();
You want to be very careful here. What happens if two threads, at the same time, execute:
order.invno = db.Invoicesets.Count();
You will have two orders with the same invno field, which is probably something you don't want.
I'm guessing that field should have a unique constraint on it, and is perhaps your primary key? I don't think there is any LINQ only way to do this, I think you need to make invno an Identity column, that way you'll be assured that it will increment and remain unique.
EDIT
As an aside, you could also generate a GUID for invno in your application code, but obviously you'll have to change the datatype of the invno field in your db.
I have a fact table with 8 foreign keys (referencing 8 dimensions), but even a combination of all eight keys does not uniquely identify a row. Do I need to add another attribute from the original data (i.e. "project-id" attribute, which is useless for anything), so that I can have a primary key, or I can leave fact table as it is, without a primary key?
The first rule of a fact table is to declare your grain - what uniquely identifies a row.
It sounds like you haven't declared your grain for this table. If the grain of the table is "one row per project", then you need to include project as a degenerate dimension in the table.
Every table must have a primary key. That's relational rule #1.
You can always add a surrogate key, but I like the idea of a fact table having attributes that satisfy a unique constraint. I second your idea: add more attributes until you have a unique constraint.
Along with those 8 foreign key include a simple surrogate key (like a row index) to each row. This will identify every row of the fact table uniquely
For a surrogate key you may start from an index say 1 for the first row and then increment the index by one each time you make a new entry to the fact table