ApiAuthorisation with foreign key

sblb 1,171 Reputation points
2023-03-25T19:03:34.56+00:00

Hi,

I have an application where it's necessary to have an ApiAuthorisation means to have several individual account.

Also I need to use a foreign key.

I defined modelBuilder has below :

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ActionItem>()                   
                  .HasOne(d => d.SuiviBE)
                  .WithMany(d => d.ActionItems);
        }

The associate class as define :

    public class SuiviBE
    {
        public int? SuiviBEId { get; set; }
        public List<ActionItem>? ActionItems { get; set; }
    }

   
    public class ActionItem
    {
        [Key]
        public int? ActionId { get; set; }
        public string? Tilte { get; set; }
        public string? DescriptionA { get; set; }
        public string? State { get; set; }
        public DateTime? OpenDate { get; set; }
        public DateTime? PlanDate { get; set; }
        public DateTime? CloseDate { get; set; }


        public int? SuiviBEId { get; set; }
        public SuiviBE? SuiviBE { get; set; }
    }

w/o ApiAuthorisation the Foreign Key works well. With the ApiAuthorisation it's not possible to do the migration.

I received the message : The navigation 'ActionItems' cannot be added because it targets the keyless entity type 'ActionItem'. Navigations can only target entity types with keys.

First I would like to know if is due to ApiAuthorisation. If yes how I can fix this problem?

Thanks in advance

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,559 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,856 questions
{count} vote

Accepted answer
  1. AgaveJoe 28,056 Reputation points
    2023-03-27T22:27:57.9433333+00:00

    Right now I would like to know How I can limit access to application functions to users?

    Ah, so after several posts we finally got to the actual use case...

    The first step is learning Blazor authentication and authorization option. Next pick an approach that best fits your goals.

    If you plan to stay with Duende IdentityServer then read the documentation and go through all the quick starts. The quick start are very good. Be aware, understanding how a token server (OAuth/OIDC) works takes effort.

    There's also, the official ASP.NET Core Blazor authentication and authorization. The bottom of the previous link has resources which you should read.

    The code I've provided gets you setup with the Identity API and Code First. From this point you can build a JWT authentication service using standard .NET tooling and Identity as user account store.

    https://www.prowaretech.com/articles/current/blazor/wasm/jwt-authentication-simple#!

    https://jasonwatmore.com/post/2020/08/13/blazor-webassembly-jwt-authentication-example-tutorial

    Last option I can think of is scaffolding Identity and hosting the Razor pages on server application. you still need to read the ASP.NET Core Blazor authentication and authorization so you understand how to handle the authentication cookie in Blazor.

    Scaffold Identity in ASP.NET Core projects

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Michael Washington 911 Reputation points MVP
    2023-03-26T15:53:37.8066667+00:00

    Try changing:

    public int? ActionId { get; set; }

    to:

    public int ActionId { get; set; }


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. hender ricard 0 Reputation points
    2023-03-26T15:59:48.1866667+00:00
    Header 1 Header 2
    Cell 1 Cell 2
    Cell 3 Cell 4
    0 comments No comments

  4. AgaveJoe 28,056 Reputation points
    2023-03-27T11:07:11.2066667+00:00

    If I understand you're trying to manually add Identity to the server project in a Blazor WASM hosted application.

    Configuration

    builder.Services.AddDbContext<ApplicationDbContext>(options =>
           options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnectionString")));
    
    builder.Services.AddIdentity<IdentityUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddEntityFrameworkStores<ApplicationDbContext>();
    

    Context

        public class ApplicationDbContext : IdentityDbContext<IdentityUser>
        {
            public ApplicationDbContext(DbContextOptions options) : base(options)
            {
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
                modelBuilder.Entity<ActionItem>()
                    .HasOne(p => p.SuiviBE)
                    .WithMany(b => b.ActionItems);
            }
    
            public DbSet<SuiviBE> SuiviBEs { get; set; }
            public DbSet<ActionItem> ActionItems { get; set; }
        }
    

    I changed the models to follow standard conventions found in the openly published reference documentation.

        public class SuiviBE
        {
            public int SuiviBEId { get; set; }
            public string? Metier { get; set; }
            public string? ECR { get; set; }
            public string? ECO { get; set; }
            public string? FEE { get; set; }
            public string? Nadt { get; set; }
            public string? Prio { get; set; }
            public string? NivComplexe { get; set; }
            public DateTime? DfinP { get; set; }   // Deadline
            public DateTime? DfinR { get; set; }   // Achievement Date
            public DateTime? DValidFee { get; set; }
            public DateTime? Date { get; set; } //Initial Date
            public DateTime? DatePush { get; set; } // Push Date 
            [NotMapped]
            public string? CloseOn { get; private set; }
            public string? Statut
            {
    
                get
                {
                    if (DatePush == null)
                    {
                        string var1;
                        var1 = "Open";
                        return var1;
                    }
                    else if (DatePush != null)
                    {
                        string var2;
                        var2 = "Close";
                        return var2;
                    }
                    return CloseOn;
                }
    
                private set { }
    
            }
            public string? CodePrio { get; set; }
            public string? Description { get; set; }
            public List<ActionItem> ActionItems { get; set; }
        }
    
    
        public class ActionItem
        {
    
            public int ActionItemId { get; set; }
            public string? Tilte { get; set; }
            public string? DescriptionA { get; set; }
            public string? State { get; set; }
            public DateTime? OpenDate { get; set; }
            public DateTime? PlanDate { get; set; }
            public DateTime? CloseDate { get; set; }
    
            public int SuiviBEId { get; set; }
            public SuiviBE SuiviBE { get; set; }
        }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.