Open Closed

how to make a CreateAsync() method for Entity with key of int type? #7595


User avatar
0
fgao@primarypartnercare.com created
  • ABP Framework version: v8.2.0
  • UI Type: Blazor WASM
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

I need to use an entity with <int> type key. It is similar as "Author" entity in abp's tutorial, but instead of using <int> as key type, e.g. "public class MyEntity : FullAuditedAggregateRoot<int>{}".

The "Author" entity uses <Guid> type for key, so it can use "GuidGenerator.Create()" to assign a key before inserting. I wonder how to make a similar CreateAsync() method in "public class MyEntityManager : DomainService{}" for inserting a new instance of MyEntity to db. Can you give me a code example?

Thanks,


7 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Int key value generated by the database. You don't need to care about it.

  • User Avatar
    0
    fgao@primarypartnercare.com created

    The thing is that the entity inherits from FullAuditedAggregateRoot<int>. See below code from abp tutorial. The Author entity's internal constructor needs ": base(id)". I tried removing "Guid id", but not sure how to handle "base(id)". If I remove "base(id)", it results null values on all audited columns. Can you show me how to modify the code around "internal Author constructor"?

    public class Author : FullAuditedAggregateRoot<Guid> {......}

    private Author()
    {
        /* This constructor is for deserialization / ORM purpose */
    }
    
    internal Author(
        **Guid id,**// ????
        string name,
        DateTime birthDate,
        string? shortBio = null)
    **    : base(id)** //????
    {
        SetName(name);
        BirthDate = birthDate;
        ShortBio = shortBio;
    }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi

    just

    private Author()
    {
        /* This constructor is for deserialization / ORM purpose */
    }
    
    internal Author(
        string name,
        DateTime birthDate,
        string? shortBio = null)
        : base(0)
    {
        SetName(name);
        BirthDate = birthDate;
        ShortBio = shortBio;
    }
    
  • User Avatar
    0
    fgao@primarypartnercare.com created

    Yeah, I tested it by seeding a row. It works just as expected. Thank you!

    However, if I run Blazor and try to show this entity, it pops up an error after the list loaded: An internal error occurred during your request! There also is a console message as below:

    8 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Unable to set property 'text' on object of type 'Blazorise.TextEdit'. The error was: Specified cast is not valid. System.InvalidOperationException: Unable to set property 'text' on object of type 'Blazorise.TextEdit'. The error was: Specified cast is not valid. ---> System.InvalidCastException: Specified cast is not valid. at Microsoft.AspNetCore.Components.Reflection.PropertySetter.CallPropertySetter[TextEdit,String](Action2 setter, Object target, Object value) at Microsoft.AspNetCore.Components.Reflection.PropertySetter.SetValue(Object target, Object value) at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.<SetProperties>g__SetProperty|3_0(Object target, PropertySetter writer, String parameterName, Object value) --- End of inner exception stack trace --- at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.<SetProperties>g__SetProperty|3_0(Object target, PropertySetter writer, String parameterName, Object value) at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target) at Microsoft.AspNetCore.Components.ParameterView.SetParameterProperties(Object target) at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters) at Blazorise.BaseComponent.SetParametersAsync(ParameterView parameters) at Blazorise.BaseInputComponent1[[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<>n__0(ParameterView parameters) at Blazorise.BaseInputComponent`1.

    Any idea?

  • User Avatar
    0
    fgao@primarypartnercare.com created

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    ** Unable to set property 'text' on object of type 'Blazorise.TextEdit'. The error was: Specified cast is not valid. ---> System.InvalidCastException: Specified cast is not valid.**

    You need to check Blazorise.TextEdit component used

  • User Avatar
    0
    fgao@primarypartnercare.com created

    Got it. I always overlook the suffix of Blazorise. I fixed it and errors gone. Thank you!

Made with ❤️ on ABP v9.1.0-preview. Updated on November 18, 2024, 05:54