i have this ValueObject
:
public class Access : ValueObject<Access>
{
public string ControllName { get; set; }
public string ActionName { get; set; }
private Access()
{
}
public Access(string controllerName, string actionName)
{
ControllName = controllerName;
ActionName = actionName;
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return ControllName;
yield return ActionName;
}
}
and this is my ValueObject<Access>
:
public abstract class ValueObject<T>
where T : ValueObject<T>
{
protected abstract IEnumerable<object> GetEqualityComponents();
public override bool Equals(object obj)
{
var valueObject = obj as T;
if (ReferenceEquals(valueObject, null))
return false;
return IsEquals(valueObject);
}
private bool IsEquals(ValueObject<T> other)
{
return GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
}
public override int GetHashCode()
{
return GetEqualityComponents()
.Aggregate(1, (current, obj) => current * 23 + (obj?.GetHashCode() ?? 0));
}
public static bool operator ==(ValueObject<T> a, ValueObject<T> b)
{
if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
return true;
if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
return false;
return a.Equals(b);
}
public static bool operator !=(ValueObject<T> a, ValueObject<T> b)
{
return !(a == b);
}
}
and this is my DBContext :
public class StoreAdminPanelGetwayContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(@"Server=.; initial catalog=StoreAdminPanelGetway;integrated security=true");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(Role).Assembly);
}
public DbSet<Role> Roles { get; set; }
public DbSet<AccessLevel> AccessLevels { get; set; }
}
this is my entity :
public class AccessLevel : Entity
{
public int RoleId { get; set; }
public Access Access { get; set; }
public Role Role { get; set; }
}
but when i need to add-migration initial
databse it show me this error :
The entity type 'Access' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'.
but Access
is value object and it can not have an id . how can i solve this problem ???
builder.Property(c => c.Access) .HasConversion(c => c.Value, c => new Access(c.action, c.controller));
– Ravine