We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

CRUDModel is null when inserting

I have developed an ASP.NET Core 2.1 instance with ej2 Grid.
I did this by examining your documents and related forum posts.

Update, and delete operations are working fine. But when I want to add a new record, an error occurs.
When I checked, I saw that CRUDModel returned a null value.



Where am I making a mistake?

Attachment: Turkceleme_f3037f09.zip

5 Replies

MF Mohammed Farook J Syncfusion Team November 19, 2018 10:14 AM UTC

Hi Adem, 
 
We have analyzed your query and we have found that the typecasting is the cause of this issue. So we suggest to set the type property of the column to ‘ID’ and ‘Fiyat’ fields as ‘number’. Please refer to the below sample for your reference, 
 
Code Example: 
 
[.cshtml] 
... 
<e-grid-columns> 
 
        <e-grid-column field="ID" headerText="ID" isPrimaryKey="true" isIdentity="true" type="number" textAlign="Right" width="120"></e-grid-column> 
        <e-grid-column field="UrunKodu" headerText="Kodu" width="150"></e-grid-column> 
        <e-grid-column field="UrunAdi" headerText="Adi" width="150"></e-grid-column> 
        <e-grid-column field="Fiyat" headerText="Fiyat" textAlign="Right" format="N2" type="number" width="140"></e-grid-column> 
        <e-grid-column field="Tarih" textAlign="Right" headerText="Tarih" editType="datepickeredit" width="150"></e-grid-column> 
        <e-grid-column field="Durum" headerText="Durum" textAlign="Center" editType="booleanedit" displayAsCheckBox="true" type="boolean" width="150"></e-grid-column> 
        ...   
    </e-grid-columns> 
... 
 
 
Please get back to us for further assistance. 
 
 
Regards, 
J Mohammed Farook 



AK Adem KARA November 22, 2018 04:07 AM UTC

Hi, Mohammed. Thank you very much for your reply.
However, the problem is not type="number". The problem is the primary key with Auto Increment. We should give it a default value even if the key field is automatic increment. The problem disappeared after editing .cshtml as follows.

 <ejs-grid id="Grid" allowPaging="true" locale="tr-TR" load="onLoad" toolbar="@( new List<object>() {"Add","Edit","Delete","Update","Cancel"})">
    <e-grid-editsettings allowAdding="true" allowDeleting="true" allowEditing="true" ></e-grid-editsettings>
    <e-data-manager url="/Index?handler=DataSource" insertUrl="/Index?handler=Insert" updateUrl="/Index?handler=Update" removeUrl="/Index?handler=Delete" adaptor="UrlAdaptor"></e-data-manager>
    <e-grid-pageSettings pageCount="5" pageSize="5"></e-grid-pageSettings>
    <e-grid-columns>
        <e-grid-column field="ID" headerText="ID" isPrimaryKey="true" isIdentity="true" defaultValue="0" type="number" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="UrunKodu" headerText="Kodu" width="150"></e-grid-column>
        <e-grid-column field="UrunAdi" headerText="Adi" width="150"></e-grid-column>
        <e-grid-column field="Fiyat" headerText="Fiyat" textAlign="Right" format="N2" type="number" width="140"></e-grid-column>
        <e-grid-column field="Tarih" textAlign="Right" headerText="Tarih" editType="datepickeredit" width="150"></e-grid-column>
        <e-grid-column field="Durum" headerText="Durum" textAlign="Center" editType="booleanedit" displayAsCheckBox="true" type="boolean" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

Thus, the program can perform all CRUD operations without any problems.

But now I have a new problem:
When I created the model, I defined a Unique Key for the UrunKodu field. 

            modelBuilder.Entity().HasAlternateKey(b => b.UrunKodu);

In this case, I get the following error message when I want to change and save the abcd field.


There is no error when I use Index instead:

            modelBuilder.Entity().HasIndex(u => u.UrunKodu).IsUnique();

Why does Unique Key fail? How can I fix this problem?


Attachment: Turkceleme123_199d90f2.zip


MF Mohammed Farook J Syncfusion Team November 22, 2018 01:06 PM UTC

Hi Adem,  
 
We have validated the provided code example and reported issue,  by generally this issue happen by the following reason.  
 
Entity Framework requires a Primary Key to generate a model from the database. If there is no Primary Key on a table it will simply select the non-nullable columns as a concatenated primary key and the Entity will be read/only.  So please check and handle the primarykey in your entity model. 
 
 
 
Regards, 
J Mohammed Farook 



AK Adem KARA November 22, 2018 02:40 PM UTC

Thanks Mohammed.

When I reviewed the EF document again, I saw this Tip:

If you just want to enforce uniqueness of a column then you want a unique index rather than an alternate key, see Indexes. In EF, alternate keys provide greater functionality than unique indexes because they can be used as the target of a foreign key.

Alternate keys are typically introduced for you when needed and you do not need to manually configure them.

I wish I had looked before, then I wouldn't keep you busy. Sorry.


MF Mohammed Farook J Syncfusion Team November 23, 2018 05:52 AM UTC

Hi Adem, 
 
Thanks for your update and we are happy to hear your requirement has been achieved. 
 
Please get back to us, if you need further assistance.  
 
Regards, 
J Mohammed Farook 


Loader.
Live Chat Icon For mobile
Up arrow icon