I have some troubles using sqlite provider to write integration tests.
If I attach a tree of entities to my DbContext, I get relations broken (the same thing with SqlServer provider works).
See sample for more information.
For precision, the PK of the 3 tables are Id + TenantId. TenantId is Set only in the SaveChangesAsync method, depending on the connected User.
If I want it to work on sqlite, I have to set the TenantId on the 3 objects before attaching them to the context... Why is this different with SQL Server provider ?
public class Store {
public int Id { get; set; }
public int TenantId { get; set; }
public List<Product> Products { get; set; }
}
public class Product {
public int Id { get; set; }
public int TenantId { get; set; }
public Store Store { get; set; }
public int StoreId { get; set; }
public Category Category { get; set; }
public int CategoryId { get; set; }
}
public class Category {
public int Id { get; set; }
public int TenantId { get; set; }
public List<Product> Products { get; set; }
}
[Test]
public void Test_Create(){
var store = new Store();
store.Products = new List<Product>();
var product = new Product();
product.Category = new Category();
store.Products.Add(product);
dbContext.Stores.Add(store);
// fails, product is detached from the Store after attaching to DbContext
Assert.Single(store.Products);
}
Related
How to call multiple entities using Include method (not Join method) in Entity Framework Core? I am trying to translate this LINQ query to EF Core 5 syntax, but I do not know how to call multiple entities and join them together using include method.
var reservations = from reservation in _dbContext.Reservations
join customer in _dbContext.Users on reservation.UserId equals customer.Id
join movie in _dbContext.Movies on reservation.MovieId equals movie.Id
select new
{
Id = reservation.Id,
ReservationTime = reservation.ReservationTime,
CustomerName = customer.Id,
MovieName = movie.Name
};
I tried using multiple include and select method, but do not know how to call multiple entities and join
Here are my models
public class Reservation
{
public int Id { get; set; }
public int Qty { get; set; }
public double Price { get; set; }
public string Phone { get; set; }
public DateTime ReservationTime { get; set; }
public int MovieId { get; set; }
public int UserId { get; set; }
}
public class Movie
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Language { get; set; }
public string Duration { get; set; }
public DateTime PlayingDate { get; set; }
public DateTime PlayingTime { get; set; }
public double TicketPrice { get; set; }
public double Rating { get; set; }
public string Genre { get; set; }
public string TrailorUrl { get; set; }
public string ImageUrl { get; set; }
[NotMapped]
public IFormFile Image { get; set; }
public ICollection<Reservation> Reservations { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Role { get; set; }
public ICollection<Reservation> Reservations { get; set; }
}
Controller code:
var reservations = _dbContext.Reservations
.Include(r => r.Id)
.Include(c => c.User)
.Select(x => new { x.Id, x.ReservationTime, x.User, x.User.Name });
If add to Reservation navigation properties Movie and User, your query can be simplified. Include cannot be used with Select together, it is ignored by EF translator.
var reservations = _dbContext.Reservations
.Select(r => new
{
Id = r.Id,
ReservationTime = r.ReservationTime,
CustomerName = r.User.Id,
MovieName = r.Movie.Name
});
I'm Learning Webapi so I'm trying to build a simple Api connected to SQL server and I got this error when I add new Movie data
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Movies_SuperHeroes_HeroId". The conflict occurred in database "SupersDb", table "dbo.SuperHeroes", column 'HeroId'.
I have two models :
Superhero Model:
namespace SuperHeroesApi.Models
{
public class SuperHero
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int HeroId { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[MaxLength(100)]
public string FirstName { get; set; }
[MaxLength(100)]
public string LastName { get; set; }
[MaxLength(100)]
public string City { get; set; }
}
}
Movie Model :
namespace SuperHeroesApi.Models
{
public class Movie
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int MovieId { get; set; }
[Required]
[MaxLength(100)]
public string Title { get; set; }
public int Year { get; set; }
public double Rate { get; set; }
public byte [] Poster { get; set; }
[ForeignKey("SuperHero")]
public int HeroId { get; set; }
//public string SuuperHeroName { get; set; }
public virtual SuperHero SuperHero { get; set; }
}
}
dto :
namespace SuperHeroesApi.Otds
{
public class MoviesDtos
{
public string Title { get; set; }
public int Year { get; set; }
public double Rate { get; set; }
public IFormFile Poster { get; set; }
[ForeignKey("SuperHero")]
public int HeroId { get; set; }
}
}
MoviesController:
using SuperHeroesApi.Otds;
namespace SuperHeroesApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class MoviesController : ControllerBase
{
private readonly AppDbContext _dbContext;
private new List<string> _allowedExtention = new List<string> { "jbg", "png" };
private long _maxAllowedPosterSize = 5242880;
public MoviesController(AppDbContext dbContext)
{
_dbContext = dbContext;
}
[HttpGet]
public async Task<IActionResult>GetAllAsync()
{
var movie = await _dbContext.Movies.ToListAsync();
return Ok(movie);
}
[HttpPost]
public async Task <IActionResult> CreateAsync([FromForm] MoviesDtos dto)
{
if (_allowedExtention.Contains(Path.GetExtension(dto.Poster.FileName).ToLower()))
return BadRequest();
using var dataStream = new MemoryStream();
await dto.Poster.CopyToAsync(dataStream);
var movie = new Movie
{
Title = dto.Title,
Year = dto.Year,
Rate = dto.Rate,
Poster = dataStream.ToArray(),
};
await _dbContext.AddAsync(movie);
_dbContext.SaveChanges();
return Ok(movie);
}
}
}
You probably already have existing rows before you made changes to your schema. Now that you're creating a new foreignkey HeroId in movie which cannot be null and an integer for that matter which means it will be a zero by default. It becomes a problem for the existing rows because they will try to reference a Hero entity with Id of 0 which doesn't exist. So, the obvious solution is to make the foreign key nullable and redo the migrations
[ForeignKey("SuperHero")]
public int? HeroId { get; set; }
I want to generate a junction table between user and post and I want to have the userId in the post table.
I have this code
public class Post
{
public Post()
{
this.ApplicationUser = new HashSet<ApplicationUser>();
}
public int PostId { get; set; }
public string Message { get; set; }
public DateTime MessageDate { get; set; }
public virtual ApplicationUser User { get; set; } //this is the problem
public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }
}
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public ApplicationUser()
{
this.Posts = new HashSet<Post>();
}
public virtual ICollection<Post> Posts { get; set; }
}
I get the extra junction table and many-to-many relation between user and post. But this is wrong.
public virtual ApplicationUser User { get; set; }
This generates two UserId in the post table (applicationUser_id and User_id) and Post_PostId in the User table. I just want one extra field in the Post table, FK UserId.
I want three tables like this
Post
PostId
Message
Date
UserId FK
User
UserId
And the rest of the fields in asp.net identity user
UserPosts
UserId
PostId
User table
public partial class User
{
public User()
{
this.Posts = new HashSet<Post>();
this.UserPosts = new HashSet<UserPost>();
}
public int UserId { get; set; }
public string Username { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual ICollection<UserPost> UserPosts { get; set; }
}
Post table
public partial class Post
{
public Post()
{
this.UserPosts = new HashSet<UserPost>();
}
public int PostId { get; set; }
public string Message { get; set; }
public Nullable<System.DateTime> Date { get; set; }
public Nullable<int> UserId { get; set; }
public virtual User User { get; set; }
public virtual ICollection<UserPost> UserPosts { get; set; }
}
and your mapping table, like this
your column 1) Id (pk), 2) UserId (fk) 3) PostId (fk)
using entity framework table have one primary key necessary.
UserPost table
public partial class UserPost
{
public int Id { get; set; }
public Nullable<int> UserId { get; set; }
public Nullable<int> PostId { get; set; }
public virtual Post Post { get; set; }
public virtual User User { get; set; }
}
Updated Code
modelBuilder.Entity<Post>().ToTable("userid table name");
this line add in below method of this class ApplicationDbContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
I'm new to mvc. I've creted an MVC app, in which i have used code first approach. Right now i have two tables Deal and Comment. Now i want to add a new table Category in the database and new column categoryId in Deal table.
How i can update database and model?
I'm using Sql Server 2008 R2 for Database.
I've following structure of class:
namespace FBWebApp.Models
{
public class Deal
{
public int ID { get; set; } // ID
public string Title { get; set; } // Titolo del deal
public string Description { get; set; } // Descrizione dell'annuncio
public string FacebookUID { get; set; } // UID facebook dell'utente
public string Visibility { get; set; } // Visibility
public string Category { get; set; }
public int Option1 { get; set; }
public int Option2 { get; set; }
public int Option3 { get; set; }
public int Option4 { get; set; }
public string PhotoURL { get; set; } // URL of the facebook photo profile
public string Name { get; set; } // Name of the user
public string ProfileUrl { get; set; } // URL of the facebook profile
public string Photo1 { get; set; } // URL of the Photo1 (local )
public string Photo2 { get; set; }
public string Photo3 { get; set; }
public string Photo4 { get; set; }
public string Photo5 { get; set; }
}
public class Comment
{
[Key]
public int CommentId { get; set; }
public string CommentText { get; set; }
public int ID { get; set; }
[ForeignKey("ID")]
public Deal DelNav { get; set; }
}
public class DealDBContext : DbContext
{
public DealDBContext() : base("DealDBContext") { }
public DbSet<Deal> Deals { get; set; }
public DbSet<Comment> Comments { get; set; }
}
}
Try using 'update-database -force -verbose' in the Package Manager Console.
If it doesn't work, modify the migration but typing 'add-migration somename' and it will apear in the Migrations folder.
If you are new to MVC and EF, definitely check out this tutorial. It explains all about that and everything else you need to know:
http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m1-intro&mode=live&clip=0&course=mvc4-building
first add your model :
public class Category
{
public int ID { get; set; }
public int cateName { get; set; }
}
in Deal class :
public class Deal
{
//..
[ForeignKey("CatId")]
public virtual Category Category { get; set; }
}
after Enable Migration you should use this command in console manager to update your database :
update-database
The EF code first update is not working for one to many relationship
I have 2 entities
// Save
public class Person
{
public int Id { get; set; }
public string Name{ get; set; }
public virtual List<Email> Emails { get; set; }
}
public class Email
{
public int Id { get; set; }
public int PersonId { get; set; }
public virtual Person person { get; set; }
public string EmailAddress { get; set; }
}
EFContext context = new EFContext();
Person person;
Email email;
person = new Person();
person.Name = "Rocky";
person.Emails = new List<Email>();
email = new Email { EmailAddress = "rocky#frostbitefalls.com" };
person.Emails.Add(email);
email = new Email { EmailAddress = "rocky#squirrel.com" };
person.Emails.Add(email);
context.People.Add(person);
context.SaveChanges();
// Update
person = new Person();
person.Id=1;
person.Name = "Rocky Altered";
person.Emails = new List<Email>();
email = new Email {Id=1, EmailAddress = "Altered_rocky#frostbitefalls.com" };
person.Emails.Add(email);
email = new Email {Id=2, EmailAddress = "Altered_rocky#squirrel.com" };
person.Emails.Add(email);
UpdatePerson(person);
public bool UpdatePerson(Person entity)
{
var updatePerson = GetPersonById(entity.Id);
updatePerson.Name=entity.Name;
updatePerson.Emails=entity.Emails;
DataContext.Entry<Person>(updatePerson).State = EntityState.Modified;
DataContext.SaveChanges();
DataContext.Entry<Person>(updatePerson).Reload();
}
The person with 2 email addresses are saving properly but while updating the email address of the inserted person is not working.
You should do something like this:
public class Person
{
public int Id { get; set; }
public string Name{ get; set; }
public List<Email> Emails { get; set; }
}
public class Email
{
public int Id { get; set; } //This will generate PKey.
public virtual Person person { get; set; } //This will create the FKey.
public string EmailAddress { get; set; }
}
The problem you're having is that you need to apply a ForeignKeyAttribute to any FK you explicitly include:
public class Person
{
public int Id { get; set; }
public string Name{ get; set; }
public virtual List<Email> Emails { get; set; }
}
public class Email
{
public int Id { get; set; }
[ForeignKey("Person")]
public int PersonId { get; set; }
public virtual Person Person { get; set; }
public string EmailAddress { get; set; }
}
By applying that attribute you tell EF Migrations to use "PersonId" as the FK column name for the "Person" relationship. The rest should work as-is.