Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:44:57+00:00 2026-06-16T00:44:57+00:00

I am still new in EF code first, so please be lenient with me.

  • 0

I am still new in EF code first, so please be lenient with me.

I have these entity classes:

    public class User
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string EmailAddress { get; set; }
        public string Password { get; set; }
        public virtual ICollection<Task> Tasks { get; set; }
        public virtual ICollection<Task> TaskAssignees { get; set; }

        public User()
        {
           Tasks = new List<Task>();
        }
    }

    public class Task
    {
       public int TaskId { get; set; }
       public string Name { get; set; }
       public virtual User CreateBy { get; set; }
       public int UserId { get; set; }
       public virtual ICollection<User> Assignees { get; set; }

       public Task()
       {
         Assignees = new List<User>();
       }
    }

with mapping configuration:

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        Property(u=>u.UserName)
            .IsRequired()
            .HasMaxLength(30);
        Property(u => u.EmailAddress)
            .IsRequired()
            .HasMaxLength(255);
        Property(u => u.Password)
            .IsRequired()
            .HasMaxLength(255);
    }
}

public class TaskMap : EntityTypeConfiguration<Domain.Entities.Task>
{
    public TaskMap()
    {
        Property(t => t.Name)
           .IsRequired()
           .HasMaxLength(255);
        HasRequired(t => t.CreateBy)
            .WithMany(u => u.Tasks)
            .HasForeignKey(t => t.UserId)
            .WillCascadeOnDelete(false)
            ;
        HasMany(t => t.Assignees)
            .WithMany(u => u.TaskAssignees)
            .Map(a =>
                {
                    a.ToTable("TaskAssignees");
                    a.MapLeftKey("TaskId");
                    a.MapRightKey("UserId");
                })
            ;
    }
}

and a fairly generic repository class:

    public class EntityRepository<TEntity> : IEntityRepositoryGetWithCRUD<TEntity> where TEntity : class 
    {
        internal DbContext context;
        internal DbSet<TEntity> dbSet;

        public EntityRepository(DbContext Context)
        {
            this.context = Context;
            this.dbSet = context.Set<TEntity>();
        }

        public virtual IQueryable<TEntity> All
        {
            get { return dbSet; }
        }

        public virtual IQueryable<TEntity> AllIncluding(params System.Linq.Expressions.Expression<Func<TEntity, object>>[] IncludeProperties)
        {
            IQueryable<TEntity> query = dbSet;

            foreach (var includeProperty in IncludeProperties)
            {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> Filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> OrderBy = null,
            params System.Linq.Expressions.Expression<Func<TEntity, object>>[] IncludeProperties)
        {
            IQueryable<TEntity> query = dbSet;

            if (Filter != null)
            {
                query = query.Where(Filter);
            }

            foreach (var includeProperty in IncludeProperties)
            {
                query = query.Include(includeProperty);
            }

            if (OrderBy != null)
            {
                return OrderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

        public virtual TEntity Find(int Id)
        {
            return dbSet.Find(Id);
        }

        public virtual void Insert(TEntity Entity)
        {
            dbSet.Add(Entity);
        }

        public virtual void Update(TEntity Entity)
        {
            dbSet.Add(Entity);
            context.Entry(Entity).State = EntityState.Modified;
        }

        public virtual void Delete(int Id)
        {
            var entity = dbSet.Find(Id);
            dbSet.Remove(entity);
        }

        public virtual void Delete(TEntity Entity)
        {
            if (context.Entry(Entity).State == EntityState.Detached)
            {
                dbSet.Attach(Entity);
            }
            dbSet.Remove(Entity);

        }

        public virtual void Dispose()
        {
            context.Dispose();
        }

        public virtual void Save()
        {
            context.SaveChanges();
        }

    }

With the above codes, i can insert a new task row into db smoothly.
However the problem arise when i tried to update the task with this simple code:

 Task task = repository.Find(2);

 if (task != null)
 {
    task.Name = "Test Update";
    repository.Update(task);
    repository.Save();
 }

The error said:

Violation of PRIMARY KEY constraint ‘PK_dbo.TaskAssignees’. Cannot
insert duplicate key in object ‘dbo.TaskAssignees’. The statement has
been terminated.

Could anybody give me some enlightenment please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:44:58+00:00Added an answer on June 16, 2026 at 12:44 am

    I think your problem might be in how you are calling update.

    I am not certain but I think your entity is already being managed by the context, so you should not need to add it to put it in an updated state. Does everything work if you just remove the call to update and try to save?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have declared: Discursiva[] questoesDiscursivas = new Discursiva[10]; Which is: public class Discursiva extends
Please have a look the following code import javax.swing.*; import java.awt.event.*; import java.awt.*; public
Please have a look at the following code First, Please note I am a
I am still new to Python and have been reviewing the following code not
I have test code like this: public class A : CriticalFinalizerObject { ~A() {
I'm still new to code igniter and I'm having problems getting the login system
still new to XML parsing with the iphone so i have a few questions.
I am still new to LINQ, and I have wrestled with a query for
I'm still very new to cocoa touch so please excuse any terminology that I
Im still fairly new to Django, so please explain things with that in mind.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.