I just can't find any code that changes the foreign key. How do I tell the context that the foreign key has changed so that it updates the database? I have been attempting to get this to work for 2 months now:
These are the models:
namespace MyApp.WebApi.Models
{
public class Project
{
public int ProjectId { get; set; }
public string Description { get; set; }
public string Name { get; set; }
// Foreign Key - Project Status
public virtual ProjectStatus ProjectStatus { get; set; }
}
public class ProjectStatus
{
public int ProjectStatusId { get; set; }
public string Name { get; set; }
}
public class HerculesWebApiContext : DbContext
{
public HerculesWebApiContext() : base("name=HerculesWebApiContext") { }
public DbSet<Project> Projects { get; set; }
public DbSet<ProjectStatus> ProjectStatuses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { }
}
}
This is the controller for the Web API:
namespace MyApp.WebApi.Controllers
{
public class ProjectController : ApiController
{
private HerculesWebApiContext db = new HerculesWebApiContext();
public void PutProject(int id, [FromBody]Project project)
{
if (!ModelState.IsValid) throw new HttpResponseException(HttpStatusCode.BadRequest);
if (id != project.ProjectId) throw new HttpResponseException(HttpStatusCode.BadRequest);
// I need some code here to tell EF that the FK has changed
db.Entry(project).State = EntityState.Modified;
// Try catch around this
db.SaveChanges();
}
The above code will not update the Project Status Foreign Key even if it has changed in the JSON object that is passed to the function. The object is returned in the same format that the Web API provided it but with the Project Status object updated:
[{"ProjectStatus":{"ProjectStatusId":2,"Name":"Started"},"ProjectId":3,"Description":"test description","Name":"test project"}]
I have tried:
db.Projects.Attach(db.Projects.Single(c => c.ProjectId == project.ProjectId));
((IObjectContextAdapter)db).ObjectContext
.ApplyCurrentValues("Projects", project);
I have also tried:
db.Entry(project.ProjectStatus).CurrentValues.SetValues(new ProjectStatus { ProjectStatusId = project.ProjectStatus.ProjectStatusId });
I have also tried this:
var per = new ProjectStatus { ProjectStatusId = project.ProjectStatus.ProjectStatusId }; // create the stub
db.ProjectStatuses.Attach(per);
db.Entry(project.ProjectStatus).CurrentValues.SetValues(per);
These are the table T-SQL files that are automatically created from my models:
Projects Table:
CREATE TABLE [dbo].[Projects] (
[ProjectId] INT IDENTITY (1, 1) NOT NULL,
[Description] NVARCHAR (MAX) NULL,
[Name] NVARCHAR (MAX) NULL,
[ProjectStatus_ProjectStatusId] INT NOT NULL,
CONSTRAINT [PK_dbo.Projects] PRIMARY KEY CLUSTERED ([ProjectId] ASC),
CONSTRAINT [FK_dbo.Projects_dbo.ProjectStatus_ProjectStatusId] FOREIGN KEY ([ProjectStatus_ProjectStatusId]) REFERENCES [dbo].[ProjectStatus] ([ProjectStatusId]) ON DELETE CASCADE
Project Statuses Table
CREATE TABLE [dbo].[ProjectStatus] (
[ProjectStatusId] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.ProjectStatus] PRIMARY KEY CLUSTERED ([ProjectStatusId] ASC)
);
You need to do two things. First explicitly state your primary keys for each class.
Add the [Key] attribute to
Project class, above the line public int ProjectId { get; set; }
ProjectStatus class, above the line public int ProjectStatusId { get; set; }
Secondly add the property for your foreign key to your Project class. Add this line:
public int ProjectStatusId { get; set; }
Related
Trying to do a basic one-to-many relationship but getting the below error. It's adding another accountstatusid column in the query but the duplicate has an additional 1 at the end.
Error:
Npgsql.PostgresException (0x80004005): 42703: column a.accountstatusid1 does not exist
Generated Query
SELECT a.accountid, a.accountstatusid, a.accountstatusid1, a.name
FROM account AS a
WHERE a.accountid = #__accountId_0
LIMIT 2
Database
CREATE TABLE accountstatus
(
accountstatusid smallint NOT NULL GENERATED ALWAYS AS IDENTITY,
name character varying(128) NOT NULL,
CONSTRAINT accountstatus_pkey PRIMARY KEY (accountstatusid)
)
CREATE TABLE account
(
accountid integer NOT NULL GENERATED ALWAYS AS IDENTITY,
accountstatusid smallint NOT NULL,
name character varying(256) NOT NULL,
CONSTRAINT account_pkey PRIMARY KEY (accountid),
CONSTRAINT fk_accountstatusidid FOREIGN KEY (accountstatusid)
)
Models
public class Account
{
public int AccountId { get; set; }
public string Name { get; set; }
public short AccountStatusId { get; set; }
public virtual AccountStatus AccountStatus { get; set; }
}
public class AccountStatus
{
public int AccountStatusId { get; set; }
public string Name { get; set; }
}
DataContext
public class DataContext : DbContext
{
public DbSet<AccountStatus> AccountStatuses { get; set; }
public DbSet<Account> Accounts { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AccountStatus>().ToTable("accountstatus");
modelBuilder.Entity<Account>(x =>
{
x.ToTable("account");
x.HasOne(x => x.AccountStatus);
});
}
}
The reason is that AccountStatus has Key of type int and your foreign key(Account.AccountStatusId) is of type short and the EF is not recognized Account.AccountStatusId as a foreign key to AccountStatus table and creates a new field.
If this won`t help you could try to set up a foreign key inside your model configuration.
I use in some project EF code First with TPH (in part of classes)
I have class like this:
namespace Billing.Model.Domain.Entities.BilProject
{
[Table("ProjectPriceList")]
public abstract class ProjectPriceList
{
[Key, Column("ProjectId", Order = 0)]
public int ProjectId { get; set; }
[Key, Column("PriceListId", Order = 1)]
public int PriceListId { get; set; }
[Key, Column("PriceListSourceType", Order = 2)]
public int PriceListSourceType { get; set; }
[ForeignKey("PriceListSourceType")]
public virtual BillingProjectCode PriceListSourceTypeCode { get; set; }
public int AddUsrnm { get; set; }
public DateTime AddTmstmp { get; set; }
[ForeignKey("ProjectId")]
public virtual BillingProject BillingProject { get; set; }
public virtual SnapShotPriceList SnapShotPriceList{ get; set; }
}
public class BillingProjectPriceList : ProjectPriceList
{
[ForeignKey("PriceListId")]
public virtual PriceList PriceList { get; set; }
}
public class EstimateProjectPriceList : ProjectPriceList
{
}
}
When I try to generate sql code to create a data base by
Update-Database -Script from console
I receive this SQL as result:
CREATE TABLE [dbo].[ProjectPriceList] (
[ProjectId] [int] NOT NULL,
[PriceListId] [int] NOT NULL,
[PriceListSourceType] [int] NOT NULL,
[AddUsrnm] [int] NOT NULL,
[AddTmstmp] [datetime] NOT NULL,
[Discriminator] [nvarchar](128) NOT NULL,
CONSTRAINT [PK_dbo.ProjectPriceList] PRIMARY KEY ([ProjectId], [PriceListId], [PriceListSourceType])
)
CREATE INDEX [IX_ProjectId] ON [dbo].[ProjectPriceList]([ProjectId])
CREATE INDEX [IX_PriceListId] ON [dbo].[ProjectPriceList]([PriceListId])
CREATE INDEX [IX_PriceListSourceType] ON [dbo].[ProjectPriceList]([PriceListSourceType])
ALTER TABLE [dbo].[ProjectPriceList] ADD CONSTRAINT [FK_dbo.ProjectPriceList_dbo.BillingProject_ProjectId] FOREIGN KEY ([ProjectId]) REFERENCES [dbo].[BillingProject] ([ProjectId]) ON DELETE CASCADE
ALTER TABLE [dbo].[ProjectPriceList] ADD CONSTRAINT [FK_dbo.ProjectPriceList_dbo.BillingProjectCode_PriceListSourceType] FOREIGN KEY ([PriceListSourceType]) REFERENCES [dbo].[BillingProjectCode] ([CodeId]) ON DELETE CASCADE
ALTER TABLE [dbo].[ProjectPriceList] ADD CONSTRAINT [FK_dbo.ProjectPriceList_dbo.PriceList_PriceListId] FOREIGN KEY ([PriceListId]) REFERENCES [dbo].[PriceList] ([PriceListId]) ON DELETE CASCADE
The last FK Constraint is redudant and make me trouble if i try add new EstimateProjectPriceList.
My question is
How to stop create this FK by some code Data Annotations or Fluent Api in OnModelCreating ?
Thnx for any help..
I have a set of Entities:
public class Board : EntityBase
{
public ICollection<Slot> Slots { get; set; }
}
public class Slot : EntityBase
{
public ICollection<Card> Cards { get; set; }
public string Header { get; set; }
public int BoardId { get; set; }
}
public class Card : EntityBase
{
public string Description { get; set; }
public string Title { get; set; }
public int SlotId { get; set; }
}
And corresponding database tables:
CREATE TABLE Boards
(
Id INT PRIMARY KEY,
UserId INT NOT NULL,
CONSTRAINT FK_Users_UserId FOREIGN KEY (UserId)
REFERENCES Users(Id)
)
CREATE TABLE Slots
(
Id INT PRIMARY KEY,
Header NVARCHAR(MAX),
BoardId INT NOT NULL,
CONSTRAINT FK_Slots_BoardId FOREIGN KEY (BoardId)
REFERENCES Boards(Id)
)
CREATE TABLE Cards
(
Id INT PRIMARY KEY,
Title NVARCHAR(MAX),
Description NVARCHAR(MAX),
SlotId INT NOT NULL,
CONSTRAINT FK_Cards_SlotId FOREIGN KEY (SlotId)
REFERENCES Slots(Id)
)
When attempting retrieving and instantiate a 'Board' from the database it's not populating the 'Slots' property. It seems that Entity framework is unable to recognise that there's a foreign key constraint. My understanding is that if the properties are not virtual they will be eager loaded, please correct me if i'm wrong.
Is there something that I'm missing / need to setup to make navigation properties work?
The calling code:
Context.Boards.Find(id);
My DbContext:
public class SampleContext : DbContext, IUnitOfWork
{
public SampleContext() : base("name=SampleApplication") { }
public void Save()
{
SaveChanges();
}
public DbSet<Board> Boards { get; set; }
public DbSet<Card> Cards { get; set; }
public DbSet<Slot> Slots { get; set; }
}
I have made the navigation properties virtual and loaded as follows, this is now working:
public Board GetBoard(int id)
{
var board = Context.Boards.Find(id);
Context.Entry(board)
.Collection(b => b.Slots)
.Load();
return board;
}
You must make navigation properties virtual for EF proxy to be able to override it.
And you're wrong about non-virtual properties to be eager loaded. They do not. You must load them explicitly with Include methods. Read here about it: https://msdn.microsoft.com/en-us/data/jj574232.aspx
Eager loading does not happen automatically like lazy loading does when you include the virtual keyword. You will need to use the Include() method
so something like
var graph = context.Boards.Include("Slots");
foreach(var board in graph)
{
Console.Writeline("Slot value {0}",board.Slots);
}
I have 2 classes: Client and Survey.
Each Client can have many surveys - but only one default survey.
I have defined the classes like this:
public class Client
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string ClientName { get; set; }
public Nullable<int> DefaultSurveyID { get; set; }
[ForeignKey("DefaultSurveyID")]
public virtual Survey DefaultSurvey { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
}
public class Survey
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string SurveyName { get; set; }
[Required]
public int ClientID { get; set; }
[ForeignKey("ClientID")]
public virtual Client Client { get; set; }
}
This creates the Client table as I expect:
[dbo].[Clients]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[ClientName] [nvarchar](max) NULL,
[DefaultSurveyID] [int] NULL
)
But the Survey table has an extra foreign key:
[dbo].[Surveys]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[SurveyName] [nvarchar](max) NULL,
[ClientID] [int] NOT NULL,
[Client_ID] [int] NULL
)
Why is Code First generating this relationship and how to I tell it not to?
The problem is that when you have multiple relationships between two entities, EF Code First isn't able to find out which navigation properties match up, unless, you tell it how, here is the code:
public class Client
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string ClientName { get; set; }
/****Change Nullable<int> by int?, looks better****/
public int? DefaultSurveyID { get; set; }
/****You need to add this attribute****/
[InverseProperty("ID")]
[ForeignKey("DefaultSurveyID")]
public virtual Survey DefaultSurvey { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
}
With your previous version, EF was creating that extra relationship because it didn't know that the DefaultSurvey property was referencing the ID of the Survey class, but you can let it know that, adding the attribute InverseProperty whose parameter is the name of the property in Survey you need DefaultSurvey to match with.
You can do it using code-first, but not being a code first expert I cheated :-)
1) I created the tables and relationships (as above without the extra Client_ID) in the database using SMS
2) I used Reverse Engineer Code First to create the required classes and mappings
3) I dropped the database and recreated it using context.Database.Create()
Original table defs:
CREATE TABLE [dbo].[Client](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[DefaultSurveyId] [int] NULL,
CONSTRAINT [PK_dbo.Client] PRIMARY KEY NONCLUSTERED
(
[Id] ASC
)
)
CREATE TABLE [dbo].[Survey](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[ClientId] [int] NULL,
CONSTRAINT [PK_dbo.Survey] PRIMARY KEY NONCLUSTERED
(
[Id] ASC
)
)
Plus foreign keys
ALTER TABLE [dbo].[Survey] WITH CHECK
ADD CONSTRAINT [FK_dbo.Survey_dbo.Client_ClientId] FOREIGN KEY([ClientId])
REFERENCES [dbo].[Client] ([Id])
ALTER TABLE [dbo].[Client] WITH CHECK
ADD CONSTRAINT [FK_dbo.Client_dbo.Survey_DefaultSurveyId]
FOREIGN KEY([DefaultSurveyId]) REFERENCES [dbo].[Survey] ([Id])
Code generated by reverse engineering:
public partial class Client
{
public Client()
{
this.Surveys = new List<Survey>();
}
public int Id { get; set; }
public string Name { get; set; }
public int? DefaultSurveyId { get; set; }
public virtual Survey DefaultSurvey { get; set; }
public virtual ICollection<Survey> Surveys { get; set; }
}
public partial class Survey
{
public Survey()
{
this.Clients = new List<Client>();
}
public int Id { get; set; }
public string Name { get; set; }
public int? ClientId { get; set; }
public virtual ICollection<Client> Clients { get; set; }
public virtual Client Client { get; set; }
}
public class ClientMap : EntityTypeConfiguration<Client>
{
#region Constructors and Destructors
public ClientMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Name).HasMaxLength(50);
// Table & Column Mappings
this.ToTable("Client");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.DefaultSurveyId).HasColumnName("DefaultSurveyId");
// Relationships
this.HasOptional(t => t.DefaultSurvey)
.WithMany(t => t.Clients).HasForeignKey(d => d.DefaultSurveyId);
}
#endregion
}
public class SurveyMap : EntityTypeConfiguration<Survey>
{
#region Constructors and Destructors
public SurveyMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Name).HasMaxLength(50);
// Table & Column Mappings
this.ToTable("Survey");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.ClientId).HasColumnName("ClientId");
// Relationships
this.HasOptional(t => t.Client)
.WithMany(t => t.Surveys).HasForeignKey(d => d.ClientId);
}
#endregion
}
Entity Framework does exactly what it's told to do. What you've told it is that there is both a one-to-many and a one-to-one relationship between Clients and Surveys. It generated both FKs in the Survey table in order to map both of the relationships that you've requested. It has no idea that you're trying to connect the two relationships together, nor do I think does it have the ability to deal with that.
As an alternative you might want to consider adding a IsDefaultSurvey field on the Survey object so that you can query for the default survey through the Surveys collection that you have on the Client object. You could even go one step further and put it in as a NotMapped property on the Client object so that you could still use Client.DefaultSurvey to get the correct survey, and not have to change any of your other code, as follows:
[NotMapped]
public Survey DefaultSurvey
{
get { return this.Surveys.First(s => s.IsDefaultSurvey); }
}
Please notice that adding the code below you will fix the issue.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Client>()
.HasOptional(x => x.DefaultSurvey)
.WithMany(x => x.Surveys);
.HasForeignKey(p => p.DefaultSurveyID);
{
}
suppose I have the following tables and relations:
CREATE TABLE [dbo].[Customers] (
[CustomerID] [int] IDENTITY(1,1) NOT NULL,
[Description] [nvarchar](255) NOT NULL
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED ([CustomerID] ASC)
CREATE TABLE [dbo].[Orders](
[OrderID] [int] IDENTITY(1,1) NOT NULL,
[CustomerID] [int] NOT NULL,
...
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED ([OrderID] ASC)
ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_Orders_Customers]
FOREIGN KEY([CustomerID]) REFERENCES [dbo].[Customers] ([CustomerID])
I am using some T4 templates to generate simple POCO classes with a property for each database column. This is very easy and really funny. In fact, the code I have is able to generate a single class for each table like in the following sample
public class Customer {
public int CustomerID { get; set; }
public string Description { get; set; }
}
I would like now to also create code that maps relationship between tables (1-N, 1-1) so to obtain, for example a class like this one
public class Customer {
public int CustomerID { get; set; }
public string Description { get; set; }
public IList<Order> Orders { get; set; }
}
Unfortunately my code does not work. Here it is:
foreach ( NavigationProperty navProperty in entity.NavigationProperties.Where( p => p.DeclaringType == entity ) ) {
string elementType = ((EntityType)( (RefType)navProperty.ToEndMember.TypeUsage.EdmType ).ElementType).Name;
string relationshipName = navProperty.ToEndMember.DeclaringType.FullName;
string targetRoleName = navProperty.ToEndMember.Name;
if ( navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ) {
<#=Accessibility.ForProperty( navProperty ) #> IList<<#=navProperty.Name#>> <#=navProperty.Name#> { get; set; }
}
}
Unfortunately this generate code like this one:
public class Customer {
public int CustomerID { get; set; }
public string Description { get; set; }
public IList<Orders> Orders { get; set; } // NOTE THE PLURAL IN THE TYPE NAME!!!
}
which is the entity set name not the entity name. What I have to do to solve this problem?
NOTE: I am using EF for the .NET 3.5 version. Also I am including the EF.Utility.CS.ttinclude available in Visual Studio 10.
Found the solution. This is the correct code:
foreach ( NavigationProperty navProperty in entity.NavigationProperties.Where( p => p.DeclaringType == entity ) ) {
string elementType = ((EntityType)( (RefType)navProperty.ToEndMember.TypeUsage.EdmType ).ElementType).Name;
if ( navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ) {
<#=Accessibility.ForProperty( navProperty ) #> IList<<#= elementType #>> <#=navProperty.Name#> { get; set; }
}
}
Instead of using
navProperty.name
inside of your list declaration
Can you use
navProperty.ToEndMember
Through that property I think you should be able to find the real non-pluralized class name