EF6 / MySql Net Connector
Posted by: José Luís Oliveira Santos
Date: August 07, 2015 10:38AM

- NET Framework 4.5.2
- Entity Framework 6.1.3
- MySql.Data 6.9.7
- MySql.Data.Entity 6.9.7


On the Index page of a site, I use the above code with Microsoft SQL Server, Microsoft SQL Server Compact, Firebird, PostgreSQL and SQLite with no erros. Only I need to do is to change the ConnectionString and everything works fine:

...
Dim q As Double = 0D
Using d As New MyContext
q = d.Products.Count
End Using
...

When I try to use the same code with MySql, I've the error message: "The sequence contains no matching elements".

How to fix it ?

All the code is:

Imports System
Imports System.Data
Imports System.Data.Entity
Imports System.Data.Entity.Core
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema

Public Class MyContext
Inherits DbContext

Public Overridable Property Products As DbSet(Of Product)
Public Overridable Property ProductEntryItems As DbSet(Of ProductEntryItem)
Public Overridable Property ProductEntries As DbSet(Of ProductEntry)
Public Overridable Property Phones As DbSet(Of Phone)
Public Overridable Property Supliers As DbSet(Of Suplier)

Public Sub New()
MyBase.New(MyContext.Cs)
End Sub

Private Shared Function Cs() As String
Database.SetInitializer(Of DContext)(New DropCreateDatabaseIfModelChanges(Of MyContext))
Return "eics"
End Function

Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
End Sub
End Class

Public Enum SalesSections
<Display(Name:="General")>
<Description("Generic Pocket Products")>
[General] = 10
<Display(Name:="Butchery")>
<Description("Butchery Related Products")>
[Butchery] = 20
<Display(Name:="Backery")>
<Description("Backery Related Products")>
[Backery] = 30
<Display(Name:="Fish and Sea Food")>
<Description("Fish and Sea Food Related Products")>
[Fishmonger] = 40
<Display(Name:="Fruits, Vegetables and Greens")>
<Description("Greens Related Products")>
[Greenery] = 50
<Display(Name:="Poultry And Eggs")>
<Description("Poultry And Eggs Related Products")>
[Poultry] = 60
<Display(Name:="Home Cleanning Items")>
<Description("Home Cleanning Items Related Products")>
[HomeCleaning] = 70
<Display(Name:="Personal Hygiene")>
<Description("Personal Hygiene Related Products")>
[PersonalHygiene] = 80
<Display(Name:="Bed, Bath and Kitchen")>
<Description("Bed, Bath and Kitchen Related Products")>
[BedAndBath] = 90
End Enum
<Table("mdl103_t0012")>
Public Class Product
<Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)>
Public Property ProductId As Integer
<Required(ErrorMessage:="The field {0} is required.")>
<Column(TypeName:="varchar", Order:=1)>
<StringLength(160, ErrorMessage:="The {0} field must have no more than {1} characters.")>
<Index("ukProductName", IsUnique:=True)>
<Display(Name:="Product's Full Name")>
Public Property Name As String
<Required(ErrorMessage:="The field {0} is required.")>
<Column(TypeName:="varchar", Order:=2)>
<StringLength(118, ErrorMessage:="The {0} field must have no more than {1} characters.")>
<Index("ukProductNCM")>
Public Property NCMCode As String
<EnumDataType(GetType(SalesSections), ErrorMEssage:="Please select a section.")>
Public Property Section As SalesSections = SalesSections.General
End Class
<Table("mdl103_t0002")>
Public Class Suplier
<Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)>
Public Property SuplierId As Integer
<Display(Name:="Name")>
<Required(ErrorMessage:="The field {0} is required.")>
<Column(TypeName:="varchar", Order:=1)>
<StringLength(160, ErrorMessage:="The {0} field must have no more than {1} characters.")>
<Index("ukProductName", IsUnique:=True)>
Public Property Name As String
<Display(Name:="Nick Name")>
<Required(ErrorMessage:="The field {0} is required.")>
<Column(TypeName:="varchar", Order:=2)>
<StringLength(118, ErrorMessage:="The {0} field must have no more than {1} characters.")>
<Index("ukProductNCM")>
Public Property NickName As String
<Display(Name:="TIN")>
<Required(ErrorMessage:="The field {0} is required.")>
<Column(TypeName:="varchar", Order:=3)>
<StringLength(30, ErrorMessage:="The {0} field must have no more than {1} characters.")>
<Index("ukProductTIN")>
Public Property TIN As String
<Display(Name:="Phone List")>
Public Overridable Property Phones As ICollection(Of Phone) = New HashSet(Of Phone)
End Class
<Table("mdl103_t0005")>
Public Class Phone
<Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)>
Public Property PhoneId As Integer
<Display(Name:="Phone Number")>
<Required(ErrorMessage:="The field {0} is required.")>
<Column(TypeName:="varchar", Order:=1)>
<StringLength(160, ErrorMessage:="The {0} field must have no more than {1} characters.")>
<Index("ukPhoneNumber", IsUnique:=True)>
Public Property Number As String
<Display(AutoGenerateField:=True, AutoGenerateFilter:=True, Name:="Regional Area Code")>
<DisplayFormat(ApplyFormatInEditMode:=True, ConvertEmptyStringToNull:=True, DataFormatString:="{0:(00)}")>
<Required(ErrorMessage:="The field {0} is required.")>
<Column(TypeName:="varchar", Order:=2)>
<StringLength(2, ErrorMessage:="The {0} field must have no more than {1} characters.")>
<Index("ukPhoneArea")>
Public Property Area As String
<Display(AutoGenerateField:=True, AutoGenerateFilter:=True, Name:="International Area Code")>
<DisplayFormat(ApplyFormatInEditMode:=True, ConvertEmptyStringToNull:=True, DataFormatString:="{0:(00)}")>
<Required(ErrorMessage:="The field {0} is required.")>
<Column(TypeName:="varchar", Order:=3)>
<StringLength(2, ErrorMessage:="The {0} field must have no more than {1} characters.")>
<Index("ukPhoneCountry")>
Public Property Country As String
<Display(Name:="Operator")>
<Required(ErrorMessage:="The field {0} is required.")>
<Column(TypeName:="varchar", Order:=4)>
<StringLength(50, ErrorMessage:="The {0} field must have no more than {1} characters.")>
<Index("ukPhoneOperator")>
Public Property CompanyName As String
End Class
<Table("mdl103_t0104")>
Public Class ProductEntry
<Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)>
Public Property EntryId As Integer
<ScaffoldColumn(False)>
Public Property SuplierId As Integer
<Required(ErrorMessage:="Please, select the {0}.")>
<Display(Name:="Suplier")>
<ForeignKey("SuplierId")>
Public Overridable Property Suplier As Suplier
Public Property Received As Date
End Class
<Table("mdl103_t0105")>
Public Class ProductEntryItem
<Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)>
Public Property Id As Integer
<ScaffoldColumn(False)>
<Required>
Public Property EntryId As Integer
<ForeignKey("EntryId")>
Public Overridable Property Entry As ProductEntry
<ScaffoldColumn(False)>
<Required>
Public Property ProductId As Integer
<Display(Name:="Product")>
<ForeignKey("ProductId")>
Public Property Product As Product
<Display(Name:="Quantity")>
<Required(ErrorMessage:="{0} is required.")>
<Range(1, 2000, ErrorMessage:="{0} must be between {2} and {1}.")>
Public Property Quantity As Double = 0D
<Display(Name:="Price (by unit)")>
<Required(ErrorMessage:="{0} is required.")>
<Range(0.01D, 20000D, ErrorMessage:="{0} must be between {2} and {1}.")>
Public Property UniquePrice As Double = 0D
End Class

Options: ReplyQuote


Subject
Written By
Posted
EF6 / MySql Net Connector
August 07, 2015 10:38AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.