I have a table which has Code
as PK, but I get the exception below in DefaultEditionCreator.cs once I try to run the application.
[Table("Test")]
public class Test: FullAuditedEntity<string>
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
new public int Id { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[MaxLength(NVarcharLength14), DataType(DataType.Text)]
public virtual string Code { get; set; }
}
Declared repository:
private readonly IRepository<Article, string> _articleRepository;
Exception:
System.InvalidOperationException: 'The specified field 'k__BackingField' of type 'int' cannot be used for the property 'Article.Id' of type 'string'. Only backing fields of types that are assignable from the property type can be used.'
I'm getting the same error while running Update-Database
and Add-Migration
.
Update 1
@aaron Thanks a lot for your help. I have tried the steps as suggested by you, but I'm getting an error while updating and deleting records.
Exception:
ERROR 2018-02-12 06:13:23,049 [30 ] Mvc.ExceptionHandling.AbpExceptionFilter - An error occurred while updating the entries. See the inner exception for details. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot update identity column 'Id'.
public async Task UpdateTest()
{
var entity = GetAll().Where(x => x.TestId == "One").FirstOrDefault();
await UpdateAsync(entity);
}
public async Task DeleteTest()
{
await DeleteAsync(x => x.TestId == "One");
}
public class Test : FullAuditedEntity
{
// PK
public string TestId { get; set; }
// Unique constraint
public int TestId2 { get; set; }
}
Update 2
I'm trying to disable SoftDelete by referring to Disable SoftDelete for AbpUserRole, but it's still doing SoftDelete, not deleting the row from DB. Please find the screenshot:
public class TestAppService : MyProjectAppServiceBase, ITestAppService
{
public Task DeleteTest()
{
using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.SoftDelete))
{
return _testRepository.DeleteTest();
}
}
}
MyDBContext.cs:
protected override void CancelDeletionForSoftDelete(EntityEntry entry)
{
if (IsSoftDeleteFilterEnabled)
{
base.CancelDeletionForSoftDelete(entry);
}
}
The solution works fine, But its giving following exception while running test case to create Test
entity.
SQLite Error 19: 'NOT NULL constraint failed: Test.Id'.
The exception is because you inherited
FullAuditedEntity<string>
, which specifies thatId
is of typestring
, and then didnew
to change the type toint
. This hiding results in a conflict for EF.Here's how you can:
Id
column of typeint
string
Code:
Generated migration:
Usage:
For completeness sake, this question is the first of a series of other Stack Overflow questions: