Migration in Entity Framework Core - not updating the database - asp.net-core

I am using a domain first approach. I created an asp.net core MVC project and added a Products.cs in the model folder
Products.cs
-----id
-----Name
-----Rate
Now I ran the following commands in package manager console
add-migration addedProducts
update-database
There are no errors in the console manager
A database was created in the MS-Sql server
Now I add another entity in the Models folder called Audit.cs
Audit.cs
--- public int Id { get; set; }
public string UserId { get; set; }
public string Type { get; set; }
public string TableName { get; set; }
public DateTime DateTime { get; set; }
public string OldValues { get; set; }
public string NewValues { get; set; }
public string AffectedColumns { get; set; }
public string PrimaryKey { get; set; }
Now I repeat the steps in the package manager console
add-migration audit
update-database
There are no error messages.
I dont see a table called Audit in the database.
Am I missing something?

Related

Podio API - Groupings are missing from Views in C#

I am working with the Podio api in C# and "Groupings" is missing from the View and ViewCreateUpdateRequest model.
When I use the sandbox call the result includes the groupings. So I'm thinking it is missing in the C# nuget package. Is there another way to access groupings for both Get View and Update View?
Sadly they are not maintaining the SDK for C#, but you can download their code from https://github.com/podio/podio-dotnet and update the code yourself.
That's what I did, I change the following
ItemId data type from Integer to Long
Added Groupings in View (Below is my View looks like)
public class View
{
[JsonProperty("view_id")]
public string ViewId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("created_on")]
public DateTime CreatedOn { get; set; }
[JsonProperty("sort_by")]
public string SortBy { get; set; }
[JsonProperty("sort_desc")]
public string SortDesc { get; set; }
[JsonProperty("filters")]
public JArray Filters { get; set; }
[JsonProperty("fields")]
public JObject Fields { get; set; }
[JsonProperty("groupings")]
public JObject Groupings { get; set; }
[JsonProperty("created_by")]
public ByLine CreatedBy { get; set; }
[JsonProperty("layout")]
public string Layout { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("rights")]
public string[] Rights { get; set; }
[JsonProperty("filter_id")]
public string FilterId { get; set; }
[JsonProperty("items")]
public int Items { get; set; }
}
Search for NuGet Package: Podio.Async by acl2

EntityFrameworkCore .net Core update existing database structure without deleting existing data

Is there a way to update existing database structure without deleting existing data in .net core?
it was working in .net EF6 but not in this version
am i missing something?
All I have is
namespace ProjectTemplate.Contexts
{
public class Dummy
{
[Key]
public Guid Id { get; set; }
[Required]
[Column(TypeName = "nvarchar(50)")]
public string firstName { get; set; }
public int age { get; set; }
public string otherfield { get; set; }
public string anotherField { get; set; }
}
}
and added one more property public string one { get; set; }
then it delete the entire database and create a new one :/
Have you tried using migrations?
In the package manager console run:
add-migration NameOfYourMigration
Then:
Update-Database
Or if you prefer to use the CLI:
dotnet ef migrations add NameOfYourMigration
Then:
dotnet ef database update

EF Core 2.2: migrating starts reporting that there are pending model changes for a context

Existing ASP.NET Core 2.1 app (running against netcore2.1) was migrated to ASP.NET Core 2.2 (installed the sdk and changed the target). Now, whenever I ran the app, it starts showing the traditional "There are pending model changes for ApplicationDbContext".
If I follow the instructions and try to add a migration, I've noticed that it does in fact generate a new migration file. By running a diff, I can see that it's adding these lines to the Application context snapshot:
modelBuilder.HasAnnotation("ProductVersion", "2.2.0-rtm-35687")
And it will also add the following to my entity:
b.Property<long?>("UserServiceId1");
b.Property<long?>("UserServiceServiceId");
b.Property<long?>("UserServiceUserId");
I'm not sure on where it gets the UserServiceId1 name (the entity has a UserServiceId property). Btw, here's the entity class code:
[Table("UserIdentifiers", Schema = "Gov")]
public class UserIdentifiers
{
[Required]
public long UserId { get; set; }
[Required]
public long ServiceId { get; set; }
[Required]
public long UserServiceId { get; set; }
[Required]
public long IdentifierId { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public long UserIdentifierId { get; set; }
public virtual Identifiers Identifier { get; set; }
public virtual UserServices UserService { get; set; }
}
The table that maps to this entity has a composite key built from the the UserId, ServiceId, UserServiceId, IdentifierId and UserIdentifierId. The snapshot has it defined like this:
b.HasKey("UserId", "ServiceId", "UserServiceId", "IdentifierId", "UserIdentifierId");
Oh, and yes, there are also migration files for dropping the UserServiceId column and adding the "new" UserServiceId1 column.
I'm not really an EF expert, so I'm not sure on why this stopped working after migrating from 2.1 to 2.2.
So, can anyone point me in the right direction?
btw, is there a way to disable migrations on ef core?
thanks
EDIT: adding the classes referenced by the UserIdentifiers entity (only showing the relations between classes):
// identifiers
[Table("Identifiers", Schema = "Gov")]
public class Identifiers
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public long IdentifierId { get; set; }
[Required]
public int IdentityResourceId { get; set; }
[Required]
public long ServiceId { get; set; }
public virtual Services Service { get; set; }
}
//Services
[Table("Services", Schema = "Gov")]
public class Services
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public long ServiceId { get; set; }
public virtual List<Identifiers> Identifiers { get; set; }
public virtual List<UserServices> UserServices { get; set; }
public virtual List<ClientServices> ClientServices { get; set; }
}
// userservices
[Table("UserServices", Schema = "Gov")]
public class UserServices
{
[Required]
public long UserId { get; set; }
[Required]
public long ServiceId { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public long UserServiceId { get; set; }
public virtual List<UserIdentifiers> UserIdentifiers { get; set; }
public virtual Services Service { get; set; }
public virtual ApplicationUser User { get; set; }
}
And finally, here's the configuration performed inside the OnModelCreating method:
builder.Entity<Identifiers>()
.HasKey(x => new { x.ServiceId, x.IdentifierId });
builder.Entity<UserIdentifiers>()
.HasKey(x => new { x.UserId, x.ServiceId, x.UserServiceId, x.IdentifierId, x.UserIdentifierId });
builder.Entity<UserServices>()
.HasKey(x => new { x.UserId, x.ServiceId, x.UserServiceId });
builder.Entity<ClientServices>()
.HasKey(x => new { x.ServiceId, x.ClientId, x.ClientServiceId });
A friend of mine solve it by adding the "missing" foreignkey info to the model:
[ForeignKey("ServiceId, IdentifierId")]
public virtual Identifiers Identifier { get; set; }
[ForeignKey("UserId, ServiceId, UserServiceId")]
public virtual UserServices UserService { get; set; }
And now everything works out as expected.
thanks again

Automapper and EF Navigation Properties

With ASP.NET MVC Core and Entity Framework Core I'm trying to create a simple website.
I've defined my Model:
public class Club
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual IEnumerable<Team> Teams { get; set; }
}
public class Team
{
[Key]
public int Id { get; set; }
public int ClubId { get; set; }
[MaxLength(32)]
public string Name { get; set; }
public virtual Club Club { get; set; }
}
As well as the corresponding View Models:
public class ClubViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public virtual IEnumerable<TeamViewModel> Teams { get; set; }
}
public class TeamViewModel
{
public int Id { get; set; }
public int ClubId { get; set; }
public string Name { get; set; }
public virtual ClubViewModel Club { get; set; }
}
I've defined an Automapper Profile with the corresponding mappers:
CreateMap<Club, ClubViewModel>();
CreateMap<ClubViewModel, Club>();
CreateMap<Team, TeamViewModel>();
CreateMap<TeamViewModel, Team>();
I try to load a Club entity, with the navigation property Teams included (_context.Club.Include(c => c.Teams).ToList()). This works as expected, it returns a Club with a list of Teams. But when I try to map this instance to a ClubViewModel, I get an 502.3 error and my debug session is ended immediately.
It seems like I am missing something trivial, but I simply do not see it. There's no information in the Windows Event Log and I can't find any usefull information in the IIS Express logging (%userprofile%\documents\IISExpress)
What is causing the crash?
You can't perform this mapping because it is circular. You'll have to remove this line
public virtual ClubViewModel Club { get; set; }
from your TeamViewModel and the mapping should work as expected.

Cached Fetch for User Roles from NHibernate in an MVC App

Using classes like this...
public class Login
{
public virtual Guid LoginId { get; set; }
public virtual string Name { get; set; }
public virtual string Email { get; set; }
public virtual IList<Group> Groups { get; set; }
}
public class Group
{
public virtual Guid GroupId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IList<Role> Roles { get; set; }
public virtual IList<Login> Logins { get; set; }
}
public class Role : ITerminable
{
public virtual Guid RoleId { get; set; }
public virtual string DisplayName { get; set; }
public virtual string RoleName { get; set; }
public virtual string Description { get; set; }
}
And an ERD that looks like this...
This is my current query.
var login = loginRepository.Query().Where(x => x.Name == username).FetchMany(x=>x.Groups).ThenFetchMany(x=>x.Roles).SingleOrDefault();
return login.Groups.SelectMany(x => x.Roles).Distinct().ToList();
The problem is that while the first request to my site is always fine and goes through as a single query for the current user's Roles, subsequent ones result in NHibernate Profiler showing lots of cached queries (one for every role). I'm not entirely sure if this is a red flag or not (I'm using SysCache2, but it's not using Database Dependencies at the moment). But I would like to try and find a way to clear itup.
Is there a way to fix this so that I don't get a cache hit for every single role on every request when the first request was just one database hit? Or as an analogy, am I misinterpreting condensation on a pipe as a leak?
Entity cache is separate from the query cache.
The query cache only stores the ids resulting from the query execution, so retrieving the entities back involves getting that list, and then getting all the values from the entity cache.