How to Convert 'Uri' into String when using entity framework core

c# entity-framework-core

Question

I want to create database tables using Entity framework core from POCO-classes. When I try and add a new scaffold item (option: Razor pages using Entity Framework (CRUD) in VS), there's an error saying: "No suitable constructor found for entity type 'Uri'"

Ive learnt that (amongst others) Uri has to be converted into String in the OnModelCreating method (in the class which inherits from dbContext).

DbContext class:

    public class SchoolDbContext : DbContext {
      public DbSet<Student> Students { get; set; }

      protected override void OnModelCreating(ModelBuilder modelBuilder) {
        modelBuilder.Entity<Student>()
        .Property(t => t.Homepage)
        .HasConversion(
        v => v.ToString(),
        v => new Uri(v));
      }
    }

Student class:

    public class Student {
      public int ID { get; set; }
      public Uri Homepage {get; set; }
    }

I know that the OnModelCreating method is called when the command "Add-Migration Initial" is entered. But since I tried to auto-implement the CRUD classes, I cannot enter the command "Add Migration Initial". Thanks a lot and excuse my poor english..

1
0
4/30/2019 12:48:54 PM

Accepted Answer

Ive finally found a solution to my problem: Though I did write a SchoolDbContext class which inherited from DbContext, I didnt specify that specific class to be the Context for my Database accesses. When creating a new scaffold item (Razor pages using Entity Framework (CRUD)), you are asked to specify the model class and the context class. Instead of generating a new context (which I did all the time) using the '+'-button, I had to specify my SchoolDbContext class.

0
5/2/2019 9:31:56 AM

Popular Answer

var uri = new Uri(path);
uri.AbsolutePath();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow