I have a one-to-many relationship between my table Case and my other table CaseReplies. I'm using EF Code First and now wants to delete a CaseReply from a Case object, however it seems impossible to do such thing because it just tries to remove the CaseId from the specific CaseReply record and not the record itself..
I've tried to set cascade delete/update at database level without luck...
short: Case just removes the relationship between itself and the CaseReply.. it does not delete the CaseReply.
My code:
// Case.cs (Case Object)
public class Case
{
[Key]
public int Id { get; set; }
public string Topic { get; set; }
public string Message { get; set; }
public DateTime Date { get; set; }
public Guid UserId { get; set; }
public virtual User User { get; set; }
public virtual ICollection<CaseReply> Replies { get; set; }
}
// CaseReply.cs (CaseReply Object)
public class CaseReply
{
[Key]
public int Id { get; set; }
public string Message { get; set; }
public DateTime Date { get; set; }
public int CaseId { get; set; }
public Guid UserId { get; set; }
public virtual User User { get; set; }
public virtual Case Case { get; set; }
}
// RepositoryBase.cs
public class RepositoryBase<T> : IRepository<T> where T : class
{
public IDbContext Context { get; private set; }
public IDbSet<T> ObjectSet { get; private set; }
public RepositoryBase(IDbContext context)
{
Contract.Requires(context != null);
Context = context;
if (context != null)
{
ObjectSet = Context.CreateDbSet<T>();
if (ObjectSet == null)
{
throw new InvalidOperationException();
}
}
}
public IRepository<T> Remove(T entity)
{
ObjectSet.Remove(entity);
return this;
}
public IRepository<T> SaveChanges()
{
Context.SaveChanges();
return this;
}
}
// CaseRepository.cs
public class CaseRepository : RepositoryBase<Case>, ICaseRepository
{
public CaseRepository(IDbContext context)
: base(context)
{
Contract.Requires(context != null);
}
public bool RemoveCaseReplyFromCase(int caseId, int caseReplyId)
{
Case caseToRemoveReplyFrom = ObjectSet.Include(x => x.Replies).FirstOrDefault(x => x.Id == caseId);
var delete = caseToRemoveReplyFrom.Replies.FirstOrDefault(x => x.Id == caseReplyId);
caseToRemoveReplyFrom.Replies.Remove(delete);
return Context.SaveChanges() >= 1;
}
}
Thanks in advance.
If you want EF to remove CaseReply from DB when you remove it from parent's object collection, you have to use Identifying relations. It means that your CaseReply must have composite primary key based on Id and CaseId columns.
I believe you are using EF CT4? I had the same problem too and hated it. I think your code is totally fine.
Since your association is one to many, the only solution is to setting cascading delete at database side. <http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx>
The above solution works for deleting orphans on deleting their parent record. I hope it will solve your issue too.
You have to remove from context.Replies if you want to delete it in db.
What you are doing right now is removing it from case's replies collection which removes the association. So you have to call:
context.Replies.Remove(delete)
Related
I see this error when updating an Application. The object has two external connections: ApplicationVisitors and ApplicationPlatforms. The properties in Application have been updated, but external connections was not be updated.
What am I doing wrong? How to update Application correctly?
Route
[Route("update")]
[HttpPut]
public async Task<IActionResult> UpdateApplication(ApplicationDTO applicationDTO)
{
if (!ModelState.IsValid)
return BadRequest("Модель не валидна!");
await applicationService.UpdateApplication(applicationDTO);
return Ok();
}
Service
public async Task UpdateApplication(ApplicationDTO applicationDTO)
{
var visitors = Mapper.ToVisitors(applicationDTO.ApplicationVisitors);
var visitorsToCreate = visitors.Where(w => w.Id == 0).ToList();
var createdVisitors = visitors.Where(w => w.Id > 0).ToList();
var resultCreateVisitors = await _wrapper.Visitor.CreateVisitorsAsync(visitorsToCreate);
createdVisitors.AddRange(resultCreateVisitors);
applicationDTO.ApplicationVisitors = Mapper.ToVisitors(createdVisitors);
await _wrapper.Application.UpdateAsync(Mapper.ToApplication(applicationDTO));
}
Repository method
public async Task UpdateAsync(Application application)
{
Update(application);
await SaveAsync();
}
BaseRepository
public void Update(T entity)
{
_repositoryContext.Set<T>().Attach(entity);
_repositoryContext.Entry(entity).State = EntityState.Modified;
}
public async Task SaveAsync()
{
await _repositoryContext.SaveChangesAsync();
}
I have not any exeption in debug. Application was filled by ID, but Platform and Visitor in collections ApplicationPlatforms and ApplicationVisitors does not filling by foreign key.
References is existing in classes.
view result Attach
Application
public class Application
{
public int Id { get; set; }
[Column(TypeName="date")]
public DateTime DateStart { get; set; }
[Column(TypeName = "date")]
public DateTime DateEnd { get; set; }
public int ApproverId { get; set; }
public User Approver { get; set; }
public int StatusId { get; set; }
public ApplicationStatus Status { get; set; }
public string VisitPurpose { get; set; }
public DateTime CreatedAt { get; set; }
public int AuthorId { get;set; }
public User Author { get; set; }
public IList<Draft> Drafts { get; set; }
public IList<ApplicationVisitor> ApplicationVisitors { get; set; }
public IList<ApplicationPlatform> ApplicationPlatforms { get; set; }
public IList<Pass> Passes { get; set; }
}
ApplicationVisitor
public class ApplicationVisitor
{
public int ApplicationId { get; set; }
[ForeignKey("ApplicationId")]
public Application Application { get; set; }
public int VisitorId { get; set; }
[ForeignKey("VisitorId")]
public Visitor Visitor { get; set; }
}
ApplicationPlatform
public class ApplicationPlatform
{
public int ApplicationId { get; set; }
[ForeignKey("ApplicationId")]
public Application Application { get; set; }
public int PlatformId { get; set; }
[ForeignKey("PlatformId")]
public Platform Platform { get; set; }
}
UPD: 22.10.2019. This is my solution what working for me on base selected answer!
I rewrited update method in ApplicationRepository
public async Task UpdateAsync(Application application)
{
var app = await GetAsync(application.Id);
app.DateStart = application.DateStart;
app.DateEnd = application.DateEnd;
app.ApproverId = application.ApproverId;
app.StatusId = application.StatusId;
app.VisitPurpose = application.VisitPurpose;
app.CreatedAt = application.CreatedAt;
app.AuthorId = application.AuthorId;
app.ApplicationVisitors = application.ApplicationVisitors;
app.ApplicationPlatforms = application.ApplicationPlatforms;
Update(app);
await SaveAsync();
}
And rewrided method in my BaseRepository
public void Update(T entity)
{
_repositoryContext.Set<T>().Update(entity);
}
Although you attached the Application object and set its state to Modified, all other objects references by it will be in Unchanged state, as stated here:
For entity types with generated keys if an entity has its primary key
value set then it will be tracked in the Unchanged state. If the
primary key value is not set then it will be tracked in the Added
state. This helps ensure only new entities will be inserted. An entity
is considered to have its primary key value set if the primary key
property is set to anything other than the CLR default for the
property type.
You can either manually set the state of all referenced objects or, A better approach in my option, is to load the objects from the database (and have EF track them for changes) and modify these objects. These way EF will know the exact state for each object when it comes to save it to the database.
If I have the following simple model:
public class Company
{
public Guid CompanyId { get; set; }
public ICollection<CompanyUser> Users { get; set; }
}
public class CompanyUser
{
public Guid CompanyId { get; set; }
public Guid UserId { get; set; }
public Company Company { get; set; }
public User User { get; set; }
}
public class User
{
public Guid UserId { get; set; }
public ICollection<CompanyUser> Companies { get; set; }
}
To get a list of companies + their users + the user object I run the following query:
return await _dataContext.Companies
.Include(m => m.Users)
.ThenInclude(m => m.User)
.OrderBy(m => m.Name)
.ToListAsync();
The results work, but I use a mapper to map the results to a view model by going recurisvely through the model.
What happens is that the Company object has a reference to a list of CompanyUser, in each of those CompanyUser objects we have a Company which has a list of CompanyUser again, which just keeps repeating until we get a stack overflow.
The mapper is a very simple one:
var results = companies.ToViewModel<Company, CompanyViewModel>();
public static IList<TModel> ToViewModel<TEntity, TModel>(this IEnumerable<TEntity> entities)
where TEntity : class
where TModel : class, IViewModel<TEntity>, new()
{
return entities?.Select(entity => entity.ToViewModel<TEntity, TModel>()).ToList();
}
public static TModel ToViewModel<TEntity, TModel>(this TEntity entity)
where TEntity : class
where TModel : class, IViewModel<TEntity>, new()
{
if (entity == null)
{
return null;
}
var model = new TModel();
model.ToViewModel(entity);
return model;
}
public interface IViewModel<in TEntity>
where TEntity : class
{
void ToViewModel(TEntity entity);
}
public class CompanyViewModel : IViewModel<Company>
{
public Guid CompanyId { get; set; }
public IList<CompanyUserViewModel> Users { get; set; }
public void ToViewModel(Company entity)
{
CompanyId = entity.CompanyId;
Users = entity.Users.ToViewModel<CompanyUser, CompanyUserViewModel>();
}
}
public class CompanyUserViewModel : IViewModel<CompanyUser>
{
public Guid CompanyId { get; set; }
public Guid UserId { get; set; }
public CompanyViewModel Company { get; set; }
public UserViewModel User { get; set; }
public void ToViewModel(CompanyUser entity)
{
CompanyId = entity.CompanyId;
UserId = entity.UserId;
Company = entity.Company.ToViewModel<Company, CompanyViewModel>();
User = entity.User.ToViewModel<User, UserViewModel>();
}
}
public class UserViewModel : IViewModel<User>
{
public Guid UserId { get; set; }
public void ToViewModel(User entity)
{
UserId = entity.Id;
}
}
Is there a way to prevent these references to be resolved?
There are multiple solutions:
1) You can use automapper instead of own mapper. It has MaxDepth property which will prevents from this problem:
Mapper.CreateMap<Source, Destination>().MaxDepth(1);
2) You can remove dependencies from your entities and use shadow properties in one direction.
Are you open to changing your data model? I think the best solution would be to remove the circular reference.
If a company contains a list of users, does the User also need both the CompanyId and the Company object he is contained in? I would remove public Company Company { get; set; } from your CompanyUser object and Companies from your User object.
Your issue is that you are mapping to CompanyViewModel which then maps to CompanyUserViewModel but this then maps back again to CompanyViewModel which creates an infinite loop.
If you expect to always start at Company (to CompanyView) then remove the recursion back from CompanyUserViewModel.
public void ToViewModel(CompanyUser entity)
{
CompanyId = entity.CompanyId;
UserId = entity.UserId;
// Company = entity.Company.ToViewModel<Company, CompanyViewModel>();
User = entity.User.ToViewModel<User, UserViewModel>();
}
Alternatively do not map the relations in your ToViewModel mapping, wire up the relations after based on the Ids.
I am trying to write my first web api with .net core. I am using VS2017 and core 1.1. I've got everything working except for one of my objects (I've tried it with that last line commented and uncommented...it makes no difference):
public class Tag
{
public int ID { get; set; }
public string Name { get; set; }
public bool ShowInFilter { get; set; }
public ICollection<SubscriberTag> SubscriberTags { get; set; }
}
My repository code looks like this:
private SubscriptionContext db;
public TagRepository(SubscriptionContext context) { db = context; }
public Tag Find(int key) => db.Tags.SingleOrDefault(a => a.ID == key);
That is being called from my TagController:
private iTagRepository TagItems { get; set; }
public TagController(iTagRepository tagItems) {TagItems = tagItems; }
[HttpGet("{id}", Name = "GetTag")]
public IActionResult Get(int id) { return new ObjectResult( TagItems.Find(id) ); }
The problem is when I run it, the query that is executed is:
exec sp_executesql
N'SELECT TOP(2) [a].[ID], [a].[Name],
[a].[ShowInFilter], [a].[SubscriberID]
FROM [Tags] AS [a]
WHERE [a].[ID] = #__key_0',N'#__key_0 int',#__key_0=1
which throw and error because Tags doesn't contain a column called SubscriberID.
I've searched all my code and SubscriberID only shows up two places (in other classes which are not being used here). I have no partial classes in my entire project (saw that was an issue on a related question.)
Why is EF adding this column to its query and how do I fix it?
As requested here is the class that contains subscriberID:
public class SubscriberTag
{
public long ID { get; set; }
public long subscriberID { get; set; }
public int tagID { get; set; }
public Subscriber Subscriber { get; set; }
public Tag Tag { get; set; }
}
Subscriber class (lots of irrelevant properties removed):
public class Subscriber
{
public Subscriber()
{
//a few value initalizers/defaults
}
public long ID { get; set; }
[StringLength(200)]
public string FirstName { get; set; }
//.......
public ICollection<Subscribers.Models.Subscription> Subscriptions { get; set; }
public ICollection<Subscribers.Models.Tag> Tags { get; set; }
}
Because of the Property Tags on Subscriber:
public ICollection<Subscribers.Models.Tag> Tags { get; set; }
Entity Framework expects the Tag object to have a foreign-key to Subscriber so it builds a query with it. It looks like to configure the many-to-many relationship needs to change the property to:
public ICollection<Subscribers.Models.SubscriberTag> SubscriberTag{ get; set; }
Configuring a Many-to-Many Relationship.
Thanks for the insight Ivan
I have a lot of models in my program that do similar things but we have a database practice to give each column specific names so "PersonId" instead of "Id"
But I would like to deal with them in a generic way. So for example in a class I have included below I want to do something like:
public virtual List<TModel> GetAll(int parentId)
{
return Context.Where(i => i.ParentID == parentId).ToList();
}
But I can't do that if ParentID unless its mapped to a database column
The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported
So I am now trying to map it to a database column:
public interface IGenModel
{
int Id { get; set; }
int ParentID { get; set; }
}
public class ChildNote : BaseModel, IGenModel
{
[Key]
[DisplayName("Child Note ID")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("ChildId")]
public int Id { get; set; }
[DisplayName("Referral")]
[Column("ReferralId")]
public int ParentID { get; set; }
[DisplayName("Cinican Notes")]
public string Notes { get; set; }
[ForeignKey("ReferralId")]
public virtual Referral Referral { get; set; }
/* Finally I tried to add something like this here
[NotMapped]
[Required]
[DisplayName("Referral")]
public int ReferralId => ParentID
*/
/* Original set uup
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DisplayName("Child Note ID")]
public int ChildId { get; set; }
[Required]
[DisplayName("Referral")]
public int ReferralId { get; set; }
public int Id { get; set; }
{
get { return ChildId; }
set { ChildId = value; }
}
public int ParentID { get; set; }
{
get { return ReferralId; }
set { ReferralId = value; }
}*/
}
But when I try and build or update the database I get errors due to:
[ForeignKey("ReferralId")]
public virtual Referral Referral { get; set; }
As it says ReferralId does not map to something in the model.
Am I fighting a losing battle here? Is is simply not possible to generalise this? And just accept that all my accesses of context may have to be specialised? I am not going to be able to change the rule that we always prefix "Id" with the model name, apparently its very useful for searches within the database which makes sense to me.
and the classes I want to use
public abstract class GenLoader<TSelf, TModel> where TModel : BaseModel, IGenModel where TSelf : class, new()
{
private static TSelf instance = null;
public static TSelf Instance
{
get
{
if (instance == null)
{
instance = new TSelf();
}
return instance;
}
}
protected abstract DbSet<TModel> Context { get; }
public virtual TModel Get(int id)
{
return Context.FirstOrDefault(i => i.Id == id);
}
public virtual List<TModel> GetAll(int parentId)
{
return Context.Where(i => i.ParentID == parentId).ToList();
}
public virtual void OnAdd(TModel model)
{
}
public virtual void Add(TModel model)
{
model.LastModifiedBy = WebSecurity.CurrentUserId;
model.LastModified = DateTime.Now;
OnAdd(model);
using (TransactionScope scope = new TransactionScope())
{
Context.Add(model);
ModelContext.Current.SaveChanges();
scope.Complete();
}
}
public virtual void OnUpdate(TModel model)
{
}
public void Update(TModel model)
{
model.LastModifiedBy = WebSecurity.CurrentUserId;
model.LastModified = DateTime.Now;
OnUpdate(model);
using (TransactionScope scope = new TransactionScope())
{
ModelContext.Current.Entry(model).State = EntityState.Modified;
ModelContext.Current.SaveChanges();
scope.Complete();
}
}
}
public class ChildNotes : GenLoader<ChildNotes, ChildNote>
{
protected override DbSet<ClinicalNote> Context => ModelContext.Current.ClinicalNotes;
}
When applied to navigation property, ForeignKey attribute expects the FK property name, not the mapped db column name. So the following should fix the issue:
[ForeignKey("ParentID")]
public virtual Referral Referral { get; set; }
The alternative is, since you put many attributes on FK property, remove the ForeignKey attribute from navigation property and apply it on the FK property, in which case it expects the navigation property name as an argument:
[DisplayName("Referral")]
[Column("ReferralId")]
[ForeignKey("Referral")]
public int ParentID { get; set; }
...
public virtual Referral Referral { get; set; }
I need to delete a record of an entity but keep all the records of another entity that is related with it:
Entity record to remove is:
public class Ask
{
// Primary properties
public int Id { get; set; }
public string Title { get; set; }
public string Message { get; set; }
public DateTime DateCreated { get; set; }
}
The related records that I want to keep after deleting an Ask record is of type MessageAsk :
public class Message
{
// Primary properties
public int Id { get; set; }
public string NameFrom { get; set; }
public string EmailFrom { get; set; }
public string TelephoneFrom { get; set; }
public string Title { get; set; }
public string MessageText { get; set; }
public bool? Approved { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateRead { get; set; }
// Navigation properties
public Member MemberFrom { get; set; }
public Member MemberTo { get; set; }
public MessageType MessageType { get; set; }
public Message MessageParent { get; set; }
}
public class MessageAsk : Message
{
public Ask Ask { get; set; }
}
Resuming, I want to delete an Ask and keep all related MessageAsk's.
EDIT:
I use the service Delete:
private readonly IRepository<Ask> _askRepository;
private readonly IRepository<MessageAsk> _messageAskRepository;
public bool Delete(int askId)
{
try
{
Ask askToDelete = _askRepository.GetById(askId);
IList<MessageAsk> relatedMessageAsks = _messageAskRepository.Query.Where(m => m.Ask.Id == askId).ToList();
_askRepository.Delete(askToDelete);
_askRepository.Save();
}
catch
{
return false;
}
return true;
}
And I use a repository to Delete the Entity:
public class Repository<T> : IRepository<T> where T : class
{
protected DbContext _dataContext;
protected DbSet<T> _dbSet;
public Repository(DbContext context)
{
_dataContext = context;
_dbSet = _dataContext.Set<T>();
}
public T NewEntityInstance()
{
return _dbSet.Create();
}
public void Delete(T entity)
{
if (_dataContext.Entry(entity).State == EntityState.Detached)
{
_dbSet.Attach(entity);
}
_dbSet.Remove(entity);
}
public virtual void Delete(object id)
{
T entity = _dbSet.Find(id);
Delete(entity);
}
public T GetById(int id)
{
return _dbSet.Find(id);
}
public virtual IQueryable<T> Query
{
get
{
return _dbSet.AsNoTracking(); <------ SOURCE OF THE PROBLEM - I HAD TO REMOVE THE ASNOTRACKING OPTION TO SOLVE THE PROBLEM
}
}
}
Error I get now:
"The DELETE statement conflicted with the REFERENCE constraint "FK_Messages_Asks". The conflict occurred in database "Heelp", table "dbo.Messages", column 'Ask_Id'.
Thanks
If your relationship is optional (that is, the foreign key from MessageAsk table to Ask table allows NULL values), you can do it this way:
using (var context = new MyContext())
{
var askToDelete = context.Asks.Single(a => a.Id == askToDeleteId);
var relatedMessageAsks = context.MessageAsks
.Where(m => m.Ask.Id == askToDeleteId)
.ToList();
// or just: context.MessageAsks.Where(m => m.Ask.Id == askToDeleteId).Load();
context.Asks.Remove(askToDelete);
// or DeleteObject if you use ObjectContext
context.SaveChanges();
}
(or context.Messages.OfType<MessageAsk>()... instead of context.MessageAsks... if you don't have a set for the derived type in your context)
You don't need to set the MessageAsk.Ask property to null explicitly here. EF will do that automatically when the askToDelete is removed and update the MessageAsk with FK = NULL in the database.
It does not work if the relationship is required (no NULLs for the FK are allowed) as you would violate a referential foreign key constraint in the database when the principal (askToDelete) would be deleted. In that case you need to assign the relatedMessageAsks to another Ask before you delete the askToDelete.