Database creation and migration works in sql 2008,but it does not work in sql2012.
Error : "Column 'IpAddress' in table 'ePoll.Answer' is of a type that is invalid
for use as a key column in an index."
propery in model :
[MaxLength(39)]
[Index(IsClustered = false)]
public string IpAddress { get; set; }
Database migration works when i use [Required] attribute.
[Required]
[MaxLength(39)]
[Index(IsClustered = false)]
public string IpAddress { get; set; }
Related
I have a problem with a constraint in my database. A StairWay class uses a strongly typed property called Tread. I receive a message that the constraint does not exist when I try to update-database but the code below tells me it is created. I am using Entity Framework, SQL Server with ASP.NET Core 3.1.
Screenshot of the error message:
//Classes I have created
public class StairWay
{
public StairTread Tread { get; set; }
public int StairTreadId { get; set; }
public int StairWayId { get; set; }
}
public class StairTread
{
public int StairTreadId { get; set; }
public int StairWayId { get; set; }
}
// SQL code generated
ALTER TABLE [dbo].[StairTread]
ADD CONSTRAINT [FK_StairTread_StairCases_StairWayId]
FOREIGN KEY ([StairWayId])
REFERENCES [dbo].[StairWays] ([StairWayId]) ON DELETE CASCADE;
I have annotated some of my model classes' keys with [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
and added a migration.
When running Update-Database I keep getting following error message:
To change the IDENTITY property of a column, the column needs to be dropped and recreated.
I also tried to Update-Database with a complete new database, to no avail as the error is the same.
When changing the identity property in SQL Server Management Studio, I do not get such error but I would like to avoid a mix of code and DB first approach.
Is there a way to either
force the update to drop and recreate the column
or drop and recreate the table ?
EDIT1:
To answer a question from the comments. My model class before looked like:
public partial class MyModel
{
[Key]
public int Id { get; set; }
[MaxLength(70)]
public string Name { get; set; }
public string Description { get; set; }
...
}
This is my model class after adding the annotation:
public partial class MyModel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[MaxLength(70)]
public string Name { get; set; }
public string Description { get; set; }
...
}
For some reason I have a database with three types of tables with the same name but the prefix is different. The schema of database is this:
I must implement only the dbo.SpRicezioneSpedizioniLight tables manually and I have realized there are these two classes:
public class SpRicezioneSpedizioniLight
{
public string AudOperation { get; set; }
public Nullable<System.DateTime> AudDateLog { get; set; }
public int PROGRESSIVO { get; set; }
}
public class GemapDbContext : DbContext
{
public DbSet<SpRicezioneSpedizioniLight> SpRicezioneSpedizioniLights { get; set; }
}
How can I identify the specific table dbo.SpRicezioneSpedizioniLight? For now my connection string is:
<add name="SABIntegrationEntities" connectionString="metadata=res://*/Models.ModelTest.csdl|res://*/Models.ModelTest.ssdl|res://*/Models.ModelTest.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=SABIntegration;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
All of this works fine in a local database with a single table.
Thank you
The "prefix" of your tablename is the name of the schema this table is associated with. "dbo" is the default schema name used by EF.
You can change the default schema by using
modelBuilder.HasDefaultSchema(SchemaName);
in your OnModelCreating-Method or map your other type to the type explicitly using
modelBuilder.Entity().ToTable("SpRicezioneSpedizioniLight", "bak");
I'm trying to use the entity framework to communicate with my existing DB. This is thus DB first. Using sql server and c#. The problem is that I would like to use a sequential GUID, generated by the database.
Each time I run the code, my controller pastes the GUID assigned to lutRoughagePhysicalSiloId to the DB (most of the time lutRoughagePhysicalSiloId is just an empty GUID (000..) since I don't fill it). As far as I can see, this should not be happening since I use [DatabaseGenerated(DatabaseGeneratedOption.Identity)].
Any Ideas?
SQL code
CREATE TABLE [dbo].[lutRoughagePhysicalSiloType]
(
[RoughagePhysicalSiloTypeId] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY UNIQUE DEFAULT NEWSEQUENTIALID(),
[PhysicalSiloTypeName] NVARCHAR(50) NOT NULL UNIQUE
)
(Notice the Default value newsequentialId() that gets generated as default by sql server)
Automatic created modelview (DB first):
namespace guiIlvoDairyCowBarn.Models
{
using System;
using System.Collections.Generic;
public partial class lutConcentratePhysicalSiloType
{
public lutConcentratePhysicalSiloType()
{
this.lutConcentratePhysicalSiloes = new HashSet<lutConcentratePhysicalSilo>();
}
public System.Guid ConcentratePhysicalSiloTypeId { get; set; }
public string PhysicalSiloTypeName { get; set; }
public virtual ICollection<lutConcentratePhysicalSilo> lutConcentratePhysicalSiloes { get; set; }
}
}
Buddy class
namespace guiIlvoDairyCowBarn.Models
{
[MetadataType(typeof(lutRoughagePhysicalSiloMD))]
public partial class lutRoughagePhysicalSiloType
{
public class lutRoughagePhysicalSiloMD
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public System.Guid RoughagePhysicalSiloTypeId { get; set; }
[Required, DisplayName("Physical silo type")]
public string PhysicalSiloTypeName { get; set; }
public virtual ICollection<lutRoughagePhysicalSilo> lutRoughagePhysicalSiloes { get; set; }
}
}
}
what is a Buddy class suppose to mean?
Using both [Key] and the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] should work and ignore any user provided value. After saving to the DB the property should have the DB generated Guid.
Your class lutConcentratePhysicalSiloType has no EF data annotations and if saved would take the values you provided. NewSequentialId() is a default and without any EF data annotations the DB row will get whatever the value you specify in the C# class.
I was encountring an error when I try to add class to the database
DB.Trips.Add(trip);
I solved it by setting the navigation properties to null, but i never had to do that before and it worked just fine, so im wondering why is that, as it doesnt seem to me as a good approach and the problem might persist.
When I do the DB.SaveChanges(); I get an error. From the SQL profiler I found out that it is trying to insert a record into Countries table.
exec sp_executesql N'insert [dbo].[Countries](......
But Trips table doesn't even have Country property. There is a City property, which has Country. But why would it try to add that as well and how can I force it to insert only into Trips table ?
The data comes in via angular $http.post, is it possible its somehow related ?
Trip class city related attributes
public int CityOriginID { get; set; }
public int CityDestinationID { get; set; }
public virtual City CityDestination { get; set; }
public virtual City CityOrigin { get; set; }
City class
public partial class City
{
public int CityID { get; set; }
public string Name { get; set; }
public string CountryCode { get; set; }
public virtual Country Country { get; set; }
}
Thanks for any suggestions
Try adding a reference to your Trip entity in your City entity like
public virtual ICollection<Trip> Trips {get; set;}
to indicate your one-to-many relationship