I've created a code-fist model for entity framework core. I did an add-migration and noticed somethings weren't correct. So, I removed the migration, made my changes and tried again. After several cycles of add/remove/add/remove... I was seeing things not change.
Here the model I'm having problems with:
namespace CompetenciesDataModel {
/// <summary>
/// This entity represents the different Competencies that an employee may have, plus a comptency may have
/// have skills that are shared across competencies.
/// This entity is a look up table that can only be modifed by an administrator.
/// </summary>
public class Competency {
/// <summary>
/// The primary key for the competencies
/// </summary>
[DatabaseGenerated( DatabaseGeneratedOption.Identity )]
public Guid CompetencyId { get; set; }
/// <summary>
/// The name of the competency
/// </summary>
[Required]
public string CompetencyName { get; set; }
/// <summary>
/// The list of skills that are part of this competency
/// </summary>
public List< CompetencySkillAssoc > CompetenciesAndSkills { get; set; }
}
Notice there are three items. When I do the add-migration, the scaffolding code shows three items, except the third one is called Competencies and not CompetenciesAndSkills. It is of type Guid and then is setup as an index on a column that I didn't create. When I look at the other indexes being created they are being done on the primary key. I've even stated that the CompetencyId is a Key and I still get the same result.
The CompetenciesAndSkills is an association table for a many-to-many relationship with a different table.
There isn't any tables within the DB since it doesn't exist, is there anywhere there's a cache that I'm missing to clear? Or, am I just not understanding something else?
Thanks in advance
The Competency entity is used as a lookup table. Therefore, when I attempted to have the Competency entity as part of a different entity (call it EntityX), the migration created a Foriegn Key within the Competency entity. After realizing that this was a look up and wasn't going to change much (unless some product adminstrator edited).
EntityX had a one-to-many relationship with Competency. Therefore, I created a new entity that contained a key to EntityX and the Competency entity. When I did the migration I didn't have an extra column being made that contained the foriegn key back to EntityX.
Related
I updated to the latest version of Devart Entity Developer 6.3.569 today and the associated Salesforce .net connector. Now I am getting messages like this
The 'ForeclosureShortsaleDate' property on 'Account' could not be set to a 'null' value. You must set this property to a non-null value of type 'System.DateTime'.
This is happening when I am pulling a record from the context. I have removed many fields from the model that have generated this error because I don't need them anyway but this is not ideal. Especially of the model needs to be recreated, Can anyone explain this to me? All of the columns in question have the Nullable property set to true.
Additional Info:
Template: DBContext
I can't say that the updated version is the issue because I don't know what the Salesforce team are doing in the Sandbox but this context worked properly last week by pulling and pushing data into the sandbox. Up until till this point there have been no model changes on my end. Any help would be appreciated.
UPDATE:
I created a clean and fresh model and I get the same results.
UPDATE:
I believe it is indeed Devart Entity Developer. I checked the last version in version control and this is what the property looked like before version 6.3.569
/// <summary>
/// There are no comments for BillingLatitude in the schema.
/// </summary>
public virtual global::System.Nullable<double> BillingLatitude
{
get;
set;
}
and what it looks like with 6.3.569
/// <summary>
/// There are no comments for BillingLatitude in the schema.
/// </summary>
public virtual global::System.Double BillingLatitude
{
get;
set;
}
I'm going to revert to the last version and generate the model again.
The problem is version 6.3.569 of Entity Developer. The Model Generation is not working properly.
this is the results from 6.3.555
/// <summary>
/// There are no comments for BillingLatitude in the schema.
/// </summary>
public virtual global::System.Nullable<double> BillingLatitude
{
get;
set;
}
I am using Entity Framework 6 with PostgreSQL.
I have an entity in which I want to prevent concurrency issues, following this documentation I added a RowVersion property with [Timestamp] attribute, however after saving changes to the entity the column RowVersion value stays the same in the database.
[Timestamp]
public byte[] RowVersion { get; set; }
Am I missing something or is there another way to handle it in PostgreSQL?
Just an updated answer for EF Core in case anyone else wanders here.
The Npgsql framework has built-in support for this using the hidden system column xmin that the OP is using in his entity as a NotMapped property.
As referenced here, you can set the xmin column as a concurrency token within EF by calling the UseXminAsConcurrencyToken method on your entity within its OnModelCreating method via Fluent API (a Data Annotation is not available at this time as far as I'm aware).
For anyone already using Fluent API configurations, it's as simple as this:
public class AwesomeEntityConfiguration : IEntityTypeConfiguration<AwesomeEntity>
{
public void Configure(EntityTypeBuilder<AwesomeEntity> builder)
{
builder.UseXminAsConcurrencyToken();
}
}
/// <summary>
/// Meant to validate concurrency en database update
/// This column is updates itself in database and only works in postgresql
/// </summary>
[ConcurrencyCheck]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
//[NotMapped]
public string xmin { get; set; }
Had to add [NotMapped] attribute just for the column not to be added in the migration, commented it after database-update.
I'm using Entity Framework code-first mode, but how to auto-generate class field comments to database?
Example:
[Table("User")]
public class User
{
/// <summary>
/// Id
/// </summary>
public long Id{get;set;}
/// <summary>
/// this is name
/// </summary>
public string name{get;set;}
}
SQL should be like this:
CREATE TABLE User
(
id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'Id',
name VARCHAR(128) NOT NULL COMMENT 'this is name'
)
Does anyone have an idea how to achieve this?
To give a more specific answer besides "not." in the comments.
It's basically not possible to achieve this as comments are not passed over to the compiled object that is taken as the base for generating the migration elements.
In addition consider that the free comment section might contain a comment that might be used as a book (i.e. there's no limitation on comments but there is on comments in the database).
You might consider using a new attribute that might suit your needs, like:
[DbComment('This field is containing bla bla')]
public int FooBar {get; set;}
This might then be incorporated into the database generation process by overwriting the Sql-Generation classes.
The concern by using this approach is still that comments need to be maintained twice.
I'm trying to create a One-to-One relationship between two tables using a primary key and a unique key of the two tables (rather the primary keys).
The following is what I'd like to work.
// The principal end
public class A
{
// The primary Key
public int AId { get; set; }
// The navigation property
public virtual B B { get; set; }
}
// The dependent end
public class B
{
// The primary Key
public int BId { get; set; }
// The unique key
[Index(IsUnique = true)]
public int AId { get; set; }
// The navigation property
public virtual A A { get; set; }
}
But then I see this error:
Unable to determine the principal end of an association between the types 'A' and 'B'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
I'm quite sure a primarykey-uniquekey relationship is allowed in SQL Server. Looks like EF6 doesn't support it. EF Core 1.0 seems to, but it's not feasible to port to EF Core right now.
Why we need a need a relationship of this kind:
We have two tables A and B in production, but the one-to-one relationship, which should have been there is missing. The relationship is something we need so we can navigate from an A object to a B object with EF. Since both tables have values, we can't really make the primary key, the foreign key - we'll end up with incorrect data.
My approach to fixing this was to:
add a column
fill it with the correct A ids
make it unique
establish the 1-1 relationship
An alternate approach to fixing this is more than welcome.
This is possible with EF Core using Fluent API's .HasPrincipalKey() method. Unfortunately not supported in EF6 which is a shame as relationships over unique keys can essentially be treated the same as foreign key constrains so I'd imagine it would have been an easy addition.
Now that MS has forgot about EF6 and is focusing on EF Core, this will never happen. I really liked using the visual entity designers as it saved so much time. Looking at the thousand+ lines of code the EF Core DbContext scaffold generator spits out for me is discouraging to say the least. Sure, it's pretty when you're dealing with a demo project consisting of two cute tables but we all know this is never the case in the real world. The whole point of using an ORM is to save time but I'm not sure if having to manage thousands of lines of configuration code is any better. Just by two cents.
I am trying to configure my model to an existing database, and am running into a problem. The previous developer modeled a one-to-one relationship using a join table. If I have the following classes and database structure below, how can I map this using code first?
public class Title {
public Property Property { get; set; }
}
public class Property {
public Title TitleInsurance { get; set; }
}
tbTitle
-TitleID = PK
tbPropertyToTitle
-TitleID - FK to tbTitle.TitleID
-PropertID - FK to tbProperty.PropertyID
tbProperty
-PropertyID = PK
Code in VB.Net here, but should be easy to translate. Mark primary keys with the Key data attribute. Entity Framework will automatically look for properties named Class + ID, i.e. tbTitleID to assign as primary keys, but since that isn't applicable here, we need the Key attribute.
Overridable properties denote Navigation Properties. In C#, this should be equivalent to Virtual properties. When this navigation property is accessed, Entity Framework will automatically look for valid foreign key relations, and populate the appropriate data.
For a one-to-one relationship, Entity Framework expects that your two tables share the same primary key, as shown by TitleID here.
Public Class tbTitle
<Key()>
Public Property TitleID As Integer
...
Public Overridable Property Property As tbProperty
End Class
Public Class tbProperty
<Key()>
Public Property TitleID As Integer
...
Public Overridable Property Title As tbTitle
End Class
Looking through the fluent API, I don't see any way to map one to one relations through a join table. You might be able to fake it by setting it up as a many to many but then you would need a bit of extra code to ensure that your relation collections only ever have one item in them.