EF4 - Is there any way it can be made to support unique indexes - entity-framework-4

My primary key is a guid column and I would like to have a unique index on another column in the table. I read that EF4 doesn't do anything for unique indexes. My question is: Can I add to a partial class any code that would allow me to check for non-unique values before my data hits the database. Currently I'm using the following configuration:
Users Desktop <> wpf Datagrid <> Observable Collection <> EF4 <> SqlCe database.
Thanks in advance.
Richard

I don't think it can do this, because to track value uniqueness entity framework requires all records from the table to be read into memory, or execute database query on each value change. It is performance-ineffective way and I think entity framework does not support this.

Supporting unique key concept is definitely in scope for the next version of EF from what i have heard. But uniqueness will be enforced at the objectcontext level meaning what is currently tracked in the object context. This is the same concept for cascading delete which currently works in EF4. In cascade delete, EF only enforces cascade deletes to entities that is currently loaded in the objectcontext. It does not try to load everything from the database.

Related

EF4 Code First with existing DB and one to one/one to many on SAME type

After countless hours I finally managed to get the fluent api settings to map our complex existing SQL DB to POCO classes. There is one thing which I do not get to work and I am wondering if it might not be supported.
Class Power has:
List<Transaction> PowerTransactions;
Transaction LastPowerTransaction;
int PowerTransactionId;
Transactions is one to many where the Transaction Table has a FK pointing to Power.PowerID, that works fine.
Power has column PowerTransactionId and FK pointing to the TransactionId in the Transaction Table.
I set in fluent API:
Power has optional Last Transaction with required power
HasOptional(x => x.LastPowerTransaction).WithRequired(x => x.PowerLastPowerTransaction).Map(x => x.MapKey("PowerTransactionId"));
Powers TransactionId is 123 and PowerID is 1, when selecting Power.LastPowertransaction I get the transaction with id 1 not 123.
As the many to many does a select .. from PowerTransaction where PowerID=1, the One to One does the same thing instead of select .. from PowerTransaction where PowerID=123.
What am I missing here ?
Mystere Man's comment was not the real answer but using the mentioned tool led to the correct answer. EF4 code first only does one to one relationships with shared primary keys. The reveres engineering revealed this as NO one to one was created. Therefore my fluent mapping was wrong. This is a limitation in EF4 code first where one has to work around using one to one as "fake" one to many if the existing db does not use shared primary keys.

Why does Database First EF skip mapping some tables?

I'm using Entity Framework 4 and with a Database First binding, and EF is not generating the entities for a few of my tables. I'm not getting any errors, and no matter how many times I select the tables to generate from the "Update Model from Database" popup menu on the design surface, the same tables are still missing from the model.
I get no errors in the wizard. They just don't get generated. Any clues?
EF requires a primary key on the table. EF will not map tables for which it can't find or derive a primary key. If all columns are nullable, it can't assume a primary key. If one or more columns are not nullable, EF will evidently derive a primary key for the table.
EF will ignore table without primary keys.
Options I can think of:
Did you check the box next to those tables?
Did you previously add them, then delete their entities but keep the cache of the tables?
If so you can remove them from entity browser window and re-add them
or manually add entities and define the table they map to in mappings window.
Perhaps tables were classified as relations instead of entities?
You can manually add the entities and choose the table they map to in mappings window.
Actually, in my case, it doesn't work because I was using a hierarchyid field as a primary key and EF doesn't work with this field type, so, it didn't import the table, because a valid PK is required.
A possibility is when you're using tables with some different field types, as hierarchy in SQL Server.
Without Primary Key Tables where Skip Automatically on EF, OtherWise You Fix a Value as Not Null.

Entity Framework 4.1 Code First - How to map a string containing csv of Entity ID's (foreign keys) to the corresponding Entity

Before I submit my question, please be aware that I'm working with an existing database owned by a third party vendor, so unfortunately changing the database format is not an option.
Here's the issue: I have an Entity mapped to a database table that has a varchar column that contains one to many foreign keys in csv format. Those foreign keys correspond to the ID's of another Entity type. What I've been doing is writing a function that creates a List of ID's from that csv list and then I search for that Entity through the DBContect object. What I'd like to do is map a relationship between the entities. Is there a way to do that? Thanks!
Unfortunately there is no way to do that without changes in the database. EF is ORM tool but it is still very dependent on correctness of database design. Storing multiple values in single column is breaking even first database normal form. For EF your column containing csv data is single string value and you cannot make relation on that value.
Btw. it is even more complicated because the column cannot represent one-to-many relation in standard relational meaning - that would require dependent entities to contain Id of your master entity, not that master entity contains Ids of all dependent entities.

How should I handle maintaining row Identity for new records in Linq / Entity Framework?

What is the proper way to create a new unique shared key when using Linq and EF with SQL?
I could use a GUID upon inserting the record but I only want want my keys to have 7 digits or characters in it as they will appear in the URL of my MVC application. If this isn't an option, or it's just better with GUID's let me know.
If you do not need a unique key across machine boundaries then I would just go with a bigint/idenity that auto-increments by 1 (which is the default). You do not have to specify the identity when adding a new entity with EF, it will be auto-assigned. Once it has been assigned you can then use it as a FK.
In my opinion GUIDs are only useful in this context if you have to guarantee that the key is unique across multiple databases, i.e. you create they key on one machine (A) and want to transfer the data to another machine (B), retaining the key. This is not possible with bigint keys since machine B might have inserted other data with the same key already.

Handling lookup tables in Entity Framework v1

I am just getting started with Microsoft's Entity Framework, using it for an MVC project since MS seems to be really pushing it, and I'm running into some issues. In my database there are multiple lookup tables tied to a single table via foreign keys. Within the entity framework I am trying to combine these into one so that I have a simplified single view for this data in my model. However, this doesn't seem possible from the designer view. Is there something obvious I'm missing? Is there a way that I can edit the edmx file manually to produce this sort of model?
At the moment, Foreign keys and lookup tables in Entity Framework are a PAIN.
EF with LINQ makes getting your data super-easy, and on the surface it looks easy to update, but with lookup tables things get difficult (for now... read on...)
I'm not sure how you would "combine" your lookup tables into a single table. If each table contains a different type of "lookup entity" then IMHO they should be represented separately in your EDM. I'm guessing you're having headaches updating a record's foreign keys to the lookup tables. That's because it is a headache.
Changing foreign key values:
MyDBEntities _db = new MyDBEntities();
//get a Person
MyDBEntities.Person person = (from p in _db.Persons
where p.Id = 1
select p).First();
// This sets the foreign key value in the Person table on the PersonType field
person.PersonTypeReference = new EntityKey("MyDBEntities.PersonType", "PersonTypeId", 3)
The next release version of the Entity Framework will have a new concept called "FK Associations." This will bring back the sanity of setting the foreign key value directly rather than having to create and set an EntityKey.
HTH.

Resources