Answered by:
Asp.net Core Web Api App - OnCreated Values

Question
-
User-510451811 posted
Hi,
I used MS Lightswitch to build my apps.
Now I would like to switch to asp.net core web api to build my backend.
I just created my first project created models and context from database with enitity framework and created a controller with scaffolding wizard.
In Lightswitch it was possible to has initial values, when instance for example a new people.
Where in the project I can add code like this? I want to share this logic to different clients, so it would be great to do this on the server.
Sry for this question, but I am really new to this things.
Thanks.
Saturday, August 1, 2020 4:49 PM
Answers
-
User-474980206 posted
It works the same
https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, August 1, 2020 6:15 PM -
User711641945 posted
Hi Dragonal,
>Where in the project I can add code like this? I want to share this logic to different clients, so it would be great to do this on the server.
Did you want to set the default value for the property when you create the instance?
If so,just set the data in the model like below:
public class Task { public string status { get; set; } = "new"; }
And you could create the new instance in the controller:
public IActionResult Index() { Task NewTask = new Task(); return View(); }
Result:
Best Regards,
Rena
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 4, 2020 9:00 AM -
User711641945 posted
Hi Dragnal,
So I use a method in the controller to call it from the client to intial my data?Yes,that is.
Or is it possible to share code from the api project and the winforms client?No,it is impossible.
Best Regards,
Rena
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 6, 2020 7:27 AM
All replies
-
User-474980206 posted
It works the same
https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, August 1, 2020 6:15 PM -
User-510451811 posted
Hi,
thanks for the answer.
But I mean something different.I would like to have initial data of a class property:
Task NewTask = new Task(); NewTask.status = "new";
The status should be set on the client without writing code in the client.
I tried to do it in the constructor on the server model, but that does not work?Thanks.
Monday, August 3, 2020 8:29 AM -
User711641945 posted
Hi Dragonal,
>Where in the project I can add code like this? I want to share this logic to different clients, so it would be great to do this on the server.
Did you want to set the default value for the property when you create the instance?
If so,just set the data in the model like below:
public class Task { public string status { get; set; } = "new"; }
And you could create the new instance in the controller:
public IActionResult Index() { Task NewTask = new Task(); return View(); }
Result:
Best Regards,
Rena
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 4, 2020 9:00 AM -
User-510451811 posted
Hi Rena,
thanks for the answer.
I have my models in a aps.net core server api app.
I consume the data via odata.I would like to write the logic on the server. But if I change the model on server side lik you, the client does not know the initial value with a new instance?
Thanks.
Tuesday, August 4, 2020 9:49 AM -
User711641945 posted
Hi Dragonal,
What is your client like?
It seems you want to set the initial value on server side and the client side could read the value dynamiclly.You may misunderstood the web api.For web api,it is impossible to did what you want.This is by design.I suggest that you could use js in the client side to get the data from the server side.
Reference:
https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-javascript?view=aspnetcore-3.1
Best Regards,
Rena
Wednesday, August 5, 2020 9:33 AM -
User-510451811 posted
Hi,
Rena. At the moment I use a winforms client.
So I use a method in the controller to call it from the client to intial my data?
Or is it possible to share code from the api project and the winforms client?
Thanks.
Wednesday, August 5, 2020 2:05 PM -
User711641945 posted
Hi Dragnal,
So I use a method in the controller to call it from the client to intial my data?Yes,that is.
Or is it possible to share code from the api project and the winforms client?No,it is impossible.
Best Regards,
Rena
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 6, 2020 7:27 AM