question

entitycrafter-4780 avatar image
0 Votes"
entitycrafter-4780 asked ZhiLv-MSFT commented

Why can't the entity be created?

 # Datenbank
 * tools -> options -> uncheck prevent saving changes that require ...
 * table id -> properties -> identity spec -> yes
 * Realtionships
    
 # Backend
    
 ## ASP Framework Web API
 * ASP .NET Web Application (.NET Framework)
 * Web API
 * Configure for HTTPS -> Aushackerln
 * Add -> Folder
 * Add -> New Item -> Data -> ADO.NET Entity Data Model -> EF Designer from database -> next
   * new connection -> server name ("localhost") -> database name ("Matura") -> ok -> next
   * EF 5.0 -> next
   * Tables auswählen -> Pluralize or singularize auswählen -> Finish
 * Manage NuGet Packages -> Swashbuckle -> latest stable (5.6) -> install
 * Rebuild
 * Controllers -> Add -> Controller -> Web API -> Web API 2 Controller with actions, using EF -> Add
   * Model class -> Tabelle auswählen
   * Data context class -> Add
    
 ## ASP CORE Web API
 * ASP .NET Core Web API
 * 5.0 + Configure for Https aushackerln
 * Install Nuget Packages:
     * Microsoft.EntityFrameworkCore.SqlServer 5.x
     * Microsoft.EntityFrameworkCore.Design 5.x
     * Microsoft.EntityFrameworkCore.Tools 5.x
     * Swashbuckle.AspNetCore 5.x
 * Tools -> NuGet Package Manager -> Package Manager Console
     * `Scaffold-DbContext "Server=.\SQLExpress;Database=Matura;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models`
 * Add -> Controller -> API -> MVC Conroller with actions, using EF
 * DTO Klassen erstellen
 * In Model Klassen Konstruktor hinzufügen (DTO -> Model)
 * In Controller auf DTO ändern
 * Startup.cs -> services.AddDbContext<MaturaContext>(options => { });
    
 # Frontend
    
 ## JSF
 * Web Applikation
 * Web Profile anhackerln
 * Jakarta auswählen
 * webapp/WEB-INF -> new -> XML Conf File -> JSF Conf
 * web.xml -> javax to jakarta
     * welcome-file-list tag hinzufügen
 * Swagger Definition kopieren
     * Projektfolder -> v1.json einfügen
 * templates Folder in Projektfolder einfügen
 * pom.xml
     * dependency
     * plugin
 * mvn clean
 * mvn compile
 * target/generated-sources/openapi/src/main/java
     * -> rechtsklick -> Mark Directory as -> Generated Sources Root
 * RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:1342")).build(Api.class);
    
    
 ## WPF
 * WPF Application
 * 5.0 Current
 * Copy Swagger JSON into Project
 * Add Service Reference
 * View Generated Code -> Copy
 * New Class -> Paste
 * Delete Service Reference
 * `User user = (User)e.Row.Item;`
 * `client.UsersAsync(user).Wait();`
 * `datagrid.ItemSource = clent.UsersAllAsync().Result;`
 * `datagrid.Items.Refresh();`
 * `Binding={Binding .}`
 * `Binding={Binding Username,UpdateSourceTrigger=PropertyChanged}`
    
    
 # Caution
 * wenn Klassen neu generieren von OpenApi -> update v1.json
 * https://docs.microsoft.com/en-us/answers/questions/832694/entity-is-already-beeing-tracked-c.html
    
    
 ```java
 @RequestScoped
 @Named
 public class LoginBean implements Serializable {
     private BenutzerDTO user;
    
     @PostConstruct
     private void init() {
         user = new BenutzerDTO();
     }
    
     public String login() {
         if (user.getBenuztername().equals("admin") && user.getPasswort().equals("admin")) {
             HttpServletRequest request =
                 (HttpServletRequest) FacesContext.getCurrentInstance()
                 .getExternalContext().getRequest();
             request.getSession().setAttribute("user", user);
             return "index.xhtml?faces-redirect=true";
         }
         return "";
     }
    
     public BenutzerDTO getUser() {
         return user;
     }
    
     public void setUser(BenutzerDTO user) {
         this.user = user;
     }
 }
 ```
dotnet-aspnet-core-webapi
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why can't the entity be created?

When and where you mees this issue? Is there any error message when you are running the EF core command?

Besides, the Scaffold-DbContext command generates code for a DbContext and entity types in a database. In order for Scaffold-DbContext to generate an entity type, the database table must have a primary key. You can check the database table, whether it have a primary key or not?

0 Votes 0 ·

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

You don’t state which of the above options you are using or what is the issue or error. Assuming you are webapi core, the instructions are for generating EF classes from an existing database.

What is the error you are getting from scaffold tool? Do you have the database file?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.