I am using EF4 and trying to use POCOs and be persistent ignorant all the way through. This works ok, except for associations.
For a simple example, I have countries and countries have an associated currency. Each Country has an association (FK) which is a Currency object setup as a property, i.e.:
public class Currency
{
//three-character currency-code
public virtual string Id { get; set; }
public virtual string Name { get; set; }
//There are other primitive properties, but they're not important
}
public class Country
{
//two-character country-code
public virtual string Id { get; set; }
public virtual string Name { get; set; }
//There are other primitive properties, but they're not important
//Fixup not shown
public virtual Currency Currency { get; set;}
}
I create a new country via a POCO (auto-generated from MS POCO tt):
var currency = currencyRepository.Find("AAA"); //returns POCO for the 'AAA' currency
var country = new Country { Id = "AA", Name = "AA Test", Currency = currency};
I'm using a UnitOfWork
pattern to implement the database changes, along the lines of:
public void RegisterNew(country)
{
MyDbContext.Countries.AddObject(country);
}
However, when I call SaveChanges
, EF returns that I can't create the Country
, because the associated Currency
already exists (fails on inserting the Currency
, PK violation).
Well, ok, but I want to associate the Country
with the existing Currency
... that's why I gave it the currency.
I could add code in RegisterNew
to Attach
the existing Currency
object, however, I want a generic solution. I don't want to have to write specific code for each POCO object I want to persist that attaches existing associations.
Is there some way of telling EF that if the associated entity already exists, then use it (if the POCO entity didn't match the persistent entity, then it could throw an error)?
I'm assuming not. Without being able to do this, I'm going to have to write a lot of (to me) pointless extra code to say 'yes, this is an existing association, obviously, use the existing persisted entity) and, more annoyingly, I won't be able to write generic code to add any kind of POCO object, which is not DRY and a lot of pointless effort, from my perspective.
Perhaps I could use foreign keys instead, but then I wouldn't have the associations and it wouldn't be a persistence-agnostic ORM solution, but a rather heavy persistence-aware DAL.