How does Entity Framework Core decide which properties are going to be NOT NULL in the database? - ef-code-first

I am using Entity Framework to create a simple banking application for learning purposes. I don't understand how it decides which properties will become nullable and which become non nullable columns in the database.
For example this
model
public class Transaction
{
public int Id { get; set; }
public Account From { get; set; }
public Account To { get; set; }
[DataType(DataType.Currency)]
public decimal Amount { get; set; }
public String Currency { get; set; }
public DateTime Timestamp { get; set; }
}
generates the following
SQL
CREATE TABLE [dbo].[Transactions] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Amount] DECIMAL (18, 2) NOT NULL,
[Currency] NVARCHAR (MAX) NULL,
[FromId] INT NULL,
[Timestamp] DATETIME2 (7) NOT NULL,
[ToId] INT NULL,
CONSTRAINT [PK_Transactions] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Transactions_Accounts_FromId] FOREIGN KEY ([FromId]) REFERENCES [dbo].[Accounts] ([Id]),
CONSTRAINT [FK_Transactions_Accounts_ToId] FOREIGN KEY ([ToId]) REFERENCES [dbo].[Accounts] ([Id])
);
GO
CREATE NONCLUSTERED INDEX [IX_Transactions_FromId]
ON [dbo].[Transactions]([FromId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Transactions_ToId]
ON [dbo].[Transactions]([ToId] ASC);
Question
Why is Currency nullable but not Amount? And what about the foreign keys FromId and ToId?
Appendix
In case it is relevant, here is the Account class referenced by the Transaction class.
public class Account
{
public int Id { get; set; }
public int UserId { get; set; }
public User User { get; set; }
[InverseProperty("From")]
public ICollection<Transaction> TransactionsSend { get; set; }
[InverseProperty("To")]
public ICollection<Transaction> TransactionsReceived { get; set; }
public decimal Balance { get; set; }
}

There is a set of conventions at work here. You can fine-tune everything but the basic rules are clear and simple:
Why is Currency nullable but not Amount? And what about the foreign keys FromId and ToId?
Amount is a value-type. The C# property can't be null When you change it to decimal? the column will be nullable too.
Currency is a reference-type (string ). The From and To properties are references too. References will map to ALLOW-NULL unless you mark them as [Required]
And DateTime is also a value-type.

Related

MobileServiceClient IMobileServiceSyncTable PullAsync Resource Not Found

This error "
WindowsAzure.MobileServices The resource you are looking for has been
removed, had its name changed, or is temporarily unavailable"
Occurs when trying to retrieve records from an Azure SQL Db connected to a App Service. It occurs for below line of code
await SurveyResponseTable.PullAsync("SurveyResponse", SurveyResponseTable.CreateQuery()).ConfigureAwait(false);
where;
SurveyResponseTable = mClient.GetSyncTable<SurveyResponse>();
and
mClient = new MobileServiceClient(Constants.BackendUrl);
I have read every SO reference to this error but can find no solution. I am not sure whether the table on the Azure SQL Db has some difference to the class on the mobile client, whether a permissions problem or what.
So at this stage I am asking for suggestions as to how to debug, what to look for (can find nothing that helps me in the exception details). Or maybe how to create a table correctly on server side. I have seen when playing with the ToDoItem example that small things can stop successful connection such as misspellings, EF using plurals on server table name, lower case id etc. Tried all those things but still same error.
Azure SQL table structure;
CREATE TABLE [dbo].[SurveyResponses] (
[id] NVARCHAR (128) DEFAULT (newid()) NOT NULL,
[ThinkingStyle] NVARCHAR (128) NULL,
[ThinkingStyleA] NVARCHAR (128) NOT NULL,
[ThinkingStyleB] NVARCHAR (128) NULL,
[ResponseOrder] INT NOT NULL,
[ResponseGroup] NVARCHAR (128) NOT NULL,
[Question] NVARCHAR (128) NULL,
[ResponseA] NVARCHAR (512) NOT NULL,
[ResponseB] NVARCHAR (512) NULL,
[AzureVersion] ROWVERSION NOT NULL,
[CreatedAt] DATETIMEOFFSET (7) DEFAULT (sysutcdatetime()) NOT NULL,
[UpdatedAt] DATETIMEOFFSET (7) NULL,
[Deleted] BIT DEFAULT ((0)) NOT NULL,
CONSTRAINT [PK_dbo.SurveyResponses] PRIMARY KEY NONCLUSTERED ([id] ASC));
GO CREATE CLUSTERED INDEX [IX_CreatedAt]
ON [dbo].[SurveyResponses]([CreatedAt] ASC);
GO CREATE TRIGGER [TR_dbo_SurveyResponses_InsertUpdateDelete] ON [dbo].[SurveyResponses] AFTER INSERT, UPDATE, DELETE AS BEGIN UPDATE [dbo].[SurveyResponses] SET [dbo].[SurveyResponses].[UpdatedAt] = CONVERT(DATETIMEOFFSET, SYSUTCDATETIME()) FROM INSERTED WHERE inserted.[id] = [dbo].[SurveyResponses].[id] END
And here is the mobile client class;
public class SurveyResponse
{
public string id { get; set; }
public string ThinkingStyle { get; set; }
public string ThinkingStyleA { get; set; }
public string ThinkingStyleB { get; set; }
public int ResponseOrder { get; set; }
public string ResponseGroup { get; set; }
public string Question { get; set; }
public string ResponseA { get; set; }
public string ResponseB { get; set; }
[Version]
public string AzureVersion { get; set; }
[CreatedAt]
public DateTime CreatedAt { get; set; }
[UpdatedAt]
public DateTime UpdatedAt { get; set; }
}

.Net Core Foreign Key Field always null

We have a .net core api project. Foreign Keyed model always returning null from select queries.
DBContext is initialized with UseLazyLoadingProxies option.
Foreign key relation is defined in the table ContentTopic.
Foreign key is defined as ContentTopic->TopicId = Topic->Id
In the sample below Topic always return null.
services.AddDbContext<VaultContext>(options =>options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("DBContext")));
[Table("ContentTopic")]
public class ContentTopic
{
[Key]
public long Id { get; set; }
public long TopicId { get; set; }
public long ContentId { get; set; }
public DateTime CreateDate { get; set; }
public bool IsInBody { get; set; }
[ForeignKey("TopicId")]
public virtual Topic Topic { get; set; }
}
UseLazyLoadingProxies extension must be called from DBContext in OnConfiguring method not from Startup.cs

linq for two tables on dotvvm framework

I recently started to learn C# and DotVVM and i run to a problem. I dont know how to make linq query from two tables. I know how to make a query or linq for one table, but i got stuck with two tables.
My database of Authors
CREATE TABLE [dbo].[Autors] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Jmeno] NVARCHAR (MAX) NULL,
[Prijmeni] NVARCHAR (MAX) NULL,
[ProstredniJmeno] NVARCHAR (MAX) NULL,
[Narozeni] DATE DEFAULT (NULL) NULL,
[Umrti] DATE DEFAULT (NULL) NULL,
[Bio] NVARCHAR (MAX) NULL,
[Narodnost] NVARCHAR (MAX) NULL,
[Obrazek] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.Autors] PRIMARY KEY CLUSTERED ([ID] ASC));
And my database of Books
CREATE TABLE [dbo].[Books] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Nazev] NVARCHAR (MAX) NOT NULL,
[IdAutor] INT NOT NULL,
[Popis] NVARCHAR (MAX) NULL,
[Isbn] NCHAR (15) NULL,
[IdZanr] INT NULL,
[RokVydani] INT NULL,
[PocetStran] INT NULL,
[Obrazek] TINYINT NULL,
CONSTRAINT [PK_dbo.Books] PRIMARY KEY CLUSTERED ([Id] ASC));
And i need to create linq query to get this
namespace AbsolvetnskaPrace.Models
{
public class AutorListModel
{
public int Id { get; set; }
public string Jmeno { get; set; } //First name
public string Prijmeni { get; set; } //Last name
}
}
namespace AbsolvetnskaPrace.Models
{
public class BookListModel
{
public int Id { get; set; }
public string Nazev { get; set; } //Title
public int IdAutor { get; set; }
public string JmenoAutor { get; set; } //Author's first name
public string PrijmeniAutor { get; set; } //Author's last name
}
}
i have class of Services where i have all linq queries i use. What I need is to make list of books but get a author's name and last name.
This is a view of the list:
<div class="page-center">
<div class="page-grid-top">
<div class="student-image"></div>
<h1>{{resource: Texts.Title_BookList}}</h1>
<dot:AuthenticatedView>
<dot:RouteLink Text="{resource: Texts.Label_NewBook}" RouteName="CRUD_BookCreate" class="page-button btn-add btn-long" />
</dot:AuthenticatedView>
</div>
<dot:GridView DataSource="{value: Books}" class="page-grid">
<Columns>
<dot:GridViewTextColumn ValueBinding="{value: Nazev}" HeaderText="{resource: Texts.Label_Title}" />
<dot:GridViewTextColumn ValueBinding="{value: IdAutor}" HeaderText="{resource: Texts.Label_BookAutor}" />
<dot:GridViewTemplateColumn>
<dot:RouteLink Text="{resource: Texts.Label_Detail}" RouteName="CRUD_BookDetail" Param-Id="{{value: Id}}" />
</dot:GridViewTemplateColumn>
<dot:GridViewTemplateColumn>
<dot:RouteLink Text="{resource: Texts.Label_Edit}" RouteName="CRUD_BookEdit" Param-Id="{{value: Id}}" />
</dot:GridViewTemplateColumn>
</Columns>
<EmptyDataTemplate>
{{resource: Texts.EmptyAutorTable}}
</EmptyDataTemplate>
</dot:GridView>
</div>
And finaly the result looks like this but the numbers in "Jméno a příjmení autora" shuld by Autor's first and last name. Right now the is Author's Id.
I know that this question is really long but I would be thankfull for any advice you can get me.
your classes (entity framework model) are missing navigation properties.
You have to add properties like this:
namespace AbsolvetnskaPrace.Models
{
public class AutorListModel
{
public int Id { get; set; }
public string Jmeno { get; set; } //First name
public string Prijmeni { get; set; } //Last name
--> pubilc ICollection<BookListModel> BookListModels { get; set; }
}
public class BookListModel
{
public int Id { get; set; }
public string Nazev { get; set; } //Titleenter code here
public int IdAutor { get; set; }
--> public AutorListModel Author { get; set; }
public string JmenoAutor { get; set; } //Author's first name
public string PrijmeniAutor { get; set; } //Author's last name
}
}
Then you linq query can look like:
context.Books.Include(a=> a.Authors).Where( -- lambda condition --).Select( -- lambda transformation --).ToList()
DotVVM serializes the objects in ViewModel, so it is highly recommended to transform you EF classes into DTO (data transfer objects) which has no logic and their main purpose is to transfer data and nothing else.

Entity Framework Many To Many allow duplicates

Is it possible to insert duplicate rows in Many to Many relationship? This is my class:
public class EventPaintballBundle
{
[Key, Column(Order = 0)]
public int EventID { get; set; }
[Key, Column(Order = 1)]
public int PaintballBundleID { get; set; }
public virtual Event Event { get; set; }
public virtual PaintballBundle PaintballBundle { get; set; }
[Range(1, Int32.MaxValue)]
public int PersonCount { get; set; }
[Required]
public DateTime Data { get; set; }
}
I want to insert second row of those values. The differences are on Date Date and PersonCount value
EventPaintballBundle xx = new EventPaintballBundle() { PaintballBundleID = 1, EventID = 155, Data = DateTime.Now, PersonCount = 5 };
dc.EventPaintballBundles.Add(xx);
dc.SaveChanges();
I'm getting error while I want to insert a duplicate of two keys.
{"Violation of PRIMARY KEY constraint 'PK_dbo.EventPaintballBundles'.
Cannot insert duplicate key in object 'dbo.EventPaintballBundles'. The
duplicate key value is (155, 1).\r\nThe statement has been
terminated."}
How to solve this problem?
Create a primary key that isn't the combination of :
public int EventID { get; set; }
public int PaintballBundleID { get; set; }
The new primary should be unique and not related to anything that actually exist, this key will exist only to make your model work database wise.
It's a classic mistake : to think your primary key should represent something that exist... NO
I learned it the hard way : even if i think that some combination of existing data will stay forever unique. I don't use it. I ALWAYS create my primary key from my own design, not representing anything real.

Wrong navigation properties when using SQLite and Entity Framework 6

Lately I've been playing around with SQLite using Entity Framework but something is not very clear to me regarding the navigation properties of the generated entities after DB first approach. And more specifically, many-to-many relationships.
Note: Using ASP.NET Web Api OWIN project.
This is what I did:
I installed latest version of Entity Framework
I installed latest version of System.Data.SQLite
I used Firefox add-on to create my database. It generated my *.sqlite
Example for one of my many-to-many db definitions while creating the DB:
CREATE TABLE "Users"
(
"Id" INTEGER PRIMARY KEY NOT NULL UNIQUE ,
"IdSrvId" INTEGER NOT NULL UNIQUE ,
"FirstName" VARCHAR NOT NULL ,
"LastName" VARCHAR NOT NULL ,
"Email" VARCHAR NOT NULL ,
"About" VARCHAR NOT NULL ,
"GenderId" INTEGER NOT NULL UNIQUE ,
"BirthDate" DATETIME,
"PhoneNumber" VARCHAR
)
CREATE TABLE "UserLanguаges"
(
"Id" INTEGER PRIMARY KEY NOT NULL UNIQUE ,
"UserId" INTEGER NULL REFERENCES Users(Id),
"LanguageId" INTEGER NULL REFERENCES Languаges(Id)
)
CREATE TABLE "Langugaes"
(
"Id" INTEGER PRIMARY KEY NOT NULL UNIQUE ,
"Name" VARCHAR NOT NULL UNIQUE
)
After that, I used Visual Studio 2015 to create a Data Model using that *.sqlite file. Following this tutorial: SQLite EntityFramework 6 Tutorial
After the generation I got all of my tables as entities looking like this:
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.GroupUsers = new HashSet<GroupUser>();
this.UserLanguаges = new HashSet<UserLanguаges>();
}
public long Id { get; set; }
public long IdSrvId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string About { get; set; }
public long GenderId { get; set; }
public Nullable<System.DateTime> BirthDate { get; set; }
public string PhoneNumber { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<UserLanguаges> UserLanguаges { get; set; }
}
public partial class Languаges
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Languаges()
{
this.UserLanguаges = new HashSet<UserLanguаges>();
}
public long Id { get; set; }
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<UserLanguаges> UserLanguаges { get; set; }
}
public partial class UserLanguаges
{
public long Id { get; set; }
public Nullable<long> UserId { get; set; }
public Nullable<long> LanguageId { get; set; }
public virtual Languаges Languаges { get; set; }
public virtual User User { get; set; }
}
What worries me here, are the navigation properties inside User and Language entities. As you can see, they make a reference to the 'bridge' table helping for the many-to-many relationship but not directly to the other entity as I expected.
I expected this:
public virtual ICollection<UserLanguаges> UserLanguаges { get; set; }
to look like this:
public virtual ICollection<Languаge> Languаges { get; set; }
inside of the User entity.
How can I fix that?
The only time Entity Framework can omit the join table is if that table consists purely of the keys of the tables being joined in a many-to-many relationship. It's the presence of the Id column on this table that is causing it to generate a new entity.
The only way around this is to remove that Id column and make that table have a composite key consisting of the UserId and LanguageId keys. If you cannot change the database schema, there's no other option but take a deep breath and accept how it works.
Some additional reading on how EF handles many-to-many relationships: https://msdn.microsoft.com/en-us/library/dd742359.aspx

Resources