I am writing a model, using Code First, with two entities: 'Action' and 'Permission'.
Each Permission points to exactly one Action. No two Permissions may point to the same Action. An Action may exist without being pointed to by a Permission.
Actions should not be aware of Permissions.
My code is:
public Action
{
public Guid Id {get; set;}
public string Name {get; set;}
}
public Permission
{
public Guid Id {get; set;}
public string Name {get; set;}
public Action Action {get; set;}
}
Also, I configured Permission using the Fluent API:
modelBuilder.Entity<Permission>.HasRequired(p => p.Action).WithOptional()
.WillCascadeOnDelete();
When I try to delete an Action, I get the following error:
"Adding a relationship with an entity which is in the Deleted state is not allowed".
I tried deleting the Permission first, and then the Action. For that, i needed to fetch the Permission given the Action ID, but i get this error:
var permission = (from p in context.Permissions.Include(p => p.Action)
where p.Action.Id == actionId
select p).Single();
"Indexed properties are not supported"
What am I doing wrong? Is there a better approch to model this?
Thanks! Nir