Is there any difference between two constructors of the Microsoft.Data.SqlClient.SqlBulkCopy? - sqlbulkcopy

The Microsoft.Data.SqlClient.SqlBulkCopy type has four constructor:
public SqlBulkCopy(SqlConnection connection);
public SqlBulkCopy(string connectionString);
public SqlBulkCopy(string connectionString, SqlBulkCopyOptions copyOptions);
public SqlBulkCopy(SqlConnection connection, SqlBulkCopyOptions copyOptions, SqlTransaction externalTransaction);
Is there any differences between calling public SqlBulkCopy("someConnectionString") and public SqlBulkCopy("someConnectionString", SqlBulkCopyOptions.Default)?

No, there is no difference.
There is no value set for the field _copyOptions: https://github.com/dotnet/SqlClient/blob/master/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs#L183
Which mean the default value is 0 (equivalent to SqlBulkCopyOptions.Default): https://github.com/dotnet/SqlClient/blob/master/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopyOptions.cs#L14
The constructor public SqlBulkCopy(string connectionString, SqlBulkCopyOptions copyOptions); call the public SqlBulkCopy(string connectionString); constructor and set the _copyOptions field: https://github.com/dotnet/SqlClient/blob/master/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs#L309
So you will get exactly the same behavior

Related

The foreign key component '.' is not a declared property on type '.'

I have the following model :
Public MustInherit Class Epreuve
Public Enum EpreuveType
CourseStade = 1
ConcoursSansBarre = 2
ConcoursAvecBarre = 3
EpreuveMultiple = 4
EpreuveCombinee = 5
End Enum
Public Property EpGuid As Guid
Public Property FormatPerf As String
Public Property CodeAppel As String
Public Property Nom As String
Public Property NomReduit As String
Public Property Chrono As String
Public Property Vent As Boolean
Public Property Ajustement As Double
Public MustOverride ReadOnly Property TypeEpreuve As EpreuveType
End Class
Public MustInherit Class EpreuveStade
Inherits Epreuve
End Class
Public MustInherit Class EpreuveComposee
Inherits EpreuveStade
Implements ISousEpreuve
Public Property EpParentGuid As Guid Implements ISousEpreuve.EpParentGuid
Public Property SousEpreuves As ObservableCollection(Of EpreuveStade) Implements ISousEpreuve.SousEpreuves
End Class
Public Class EpreuveCombinee
Inherits EpreuveComposee
Public Property EpreuveParent As EpreuveComposee
Public Overrides ReadOnly Property TypeEpreuve As EpreuveType
Get
Return EpreuveType.EpreuveCombinee
End Get
End Property
End Class
EpreuveComposee and EpreuveStade has a many to may relationship.
Here is the fluent API i've done :
With modelBuilder.Entity(Of Epreuve)
.HasKey(Function(ep) ep.EpGuid)
.Property(Function(ep) ep.EpGuid).HasDatabaseGeneratedOption(ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)
.Map(Of EpreuveCombinee)(Function(f) f.Requires("TypeEpreuve").HasValue(DirectCast(Epreuve.EpreuveType.EpreuveCombinee, Integer)))
End With
With modelBuilder.Entity(Of EpreuveCombinee)
.HasRequired(Function(f) f.EpreuveParent).WithMany.HasForeignKey(Function(f) f.EpParentGuid)
End With
When I try to add-migration, I get the following error :
The foreign key component 'EpParentGuid' is not a declared property on
type 'EpreuveCombinee'. Verify that it has not been explicitly
excluded from the model and that it is a valid primitive property.
I've found it.
Public MustInherit Class EpreuveComposee
Inherits EpreuveStade
Implements ISousEpreuve
Public Property EpParentGuid As Guid Implements ISousEpreuve.EpParentGuid
Public Property EpParent As Epreuve Implements ISousEpreuve.EpParent
Public Property SousEpreuves As ObservableCollection(Of Epreuve) Implements ISousEpreuve.SousEpreuves
End Class
Public Class EpreuveCombinee
Inherits EpreuveComposee
End Class
And the fluent API :
With modelBuilder.Entity(Of EpreuveComposee)
.HasMany(Function(f) f.SousEpreuves).WithMany().Map(Sub(m)
m.ToTable("EpreuveSousEpreuves")
m.MapLeftKey("EpGuid")
m.MapRightKey("EpParentGuid")
End Sub)
End With

EF6 database first - the relationship was not loaded because the type is not available

I an using EF6 database first design in a web application. There is one entity that is involved in two 1-to-many relationships (the E2 in this example):
E1 <--- E2 ---> E3
E1 and E3 both have a navigation property for E2 (called E2s). When I try to ListView of E3, I get the error listed in the title. If I remove the navigation property for E2 from E3, everything works just fine! I've tried looking all over google to no avail. Any help/direction would be appreciated.
Added on 5/15:
tmg - hope this is what you are wanting.
This would be the 'E2' in my example:
Imports System
Imports System.Collections.Generic
Partial Public Class contact
Public Property contact_id As Integer
Public Property implementation_id As Integer
Public Property contact_type_id As Integer
Public Property name As String
Public Property user_id As String
Public Property is_main_contact As Boolean
Public Property email_address As String
Public Overridable Property contact_type As contact_type
Public Overridable Property implementation As implementation
End Class
The next two are the E1 and E3:
Imports System
Imports System.Collections.Generic
Partial Public Class contact_type
Public Property contact_type_id As Integer
Public Property contact_type_description As String
Public Overridable Property contacts As ICollection(Of contact) = New HashSet(Of contact)
End Class
Imports System
Imports System.Collections.Generic
Partial Public Class implementation
Public Property implementation_ID As Integer
Public Property user_ID As String
Public Property comments As String
Public Property creation_date As Nullable(Of Date)
Public Property customer_name As String
Public Property customer_global_name As String
Public Property customer_address As String
Public Property customer_country As String
Public Property service_order_number As String
Public Property title As String
Public Property contact_name As String
Public Overridable Property Country As Country
Public Overridable Property supportDetail As support_detail
Public Overridable Property tradeCompliance As trade_compliance
Public Overridable Property statusEffDates As ICollection(Of implementation_status_eff_date) = New HashSet(Of implementation_status_eff_date)
Public Overridable Property hwsData As hws_data
Public Overridable Property documents As ICollection(Of document) = New HashSet(Of document)
Public Overridable Property contacts As ICollection(Of contact) = New HashSet(Of contact)
End Class
This is the list view code:
Imports System.Web.ModelBinding
Public Class ImplementationList
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Function GetImplementations(<QueryString("id")> categoryId As System.Nullable(Of Integer)) As IQueryable(Of implementation)
Dim db = New RFI_SDM.RFI_DataEntities
Dim query As IQueryable(Of implementation) = db.implementations
Return query
End Function
End Class
And finally the code for RFI_DataEntities:
Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure
Partial Public Class RFI_DataEntities
Inherits DbContext
Public Sub New()
MyBase.New("name=RFI_DataEntities")
End Sub
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
Throw New UnintentionalCodeFirstException()
End Sub
Public Overridable Property Countries() As DbSet(Of Country)
Public Overridable Property implementations() As DbSet(Of implementation)
Public Overridable Property replacement_type() As DbSet(Of replacement_type)
Public Overridable Property service_level_choice() As DbSet(Of service_level_choice)
Public Overridable Property support_detail() As DbSet(Of support_detail)
Public Overridable Property tat_end_choice() As DbSet(Of tat_end_choice)
Public Overridable Property tat_measurement_basis() As DbSet(Of tat_measurement_basis)
Public Overridable Property tat_start_choice() As DbSet(Of tat_start_choice)
Public Overridable Property regions() As DbSet(Of region)
Public Overridable Property contacts() As DbSet(Of contact)
Public Overridable Property contact_type() As DbSet(Of contact_type)
Public Overridable Property hws_data() As DbSet(Of hws_data)
Public Overridable Property implementation_mgr() As DbSet(Of implementation_mgr)
Public Overridable Property implementation_service_level() As DbSet(Of implementation_service_level)
Public Overridable Property implementation_status() As DbSet(Of implementation_status)
Public Overridable Property implementation_status_eff_date() As DbSet(Of implementation_status_eff_date)
Public Overridable Property trade_compliance() As DbSet(Of trade_compliance)
Public Overridable Property implementation_type() As DbSet(Of implementation_type)
Public Overridable Property documents() As DbSet(Of document)
Public Overridable Property document_type() As DbSet(Of document_type)
Public Overridable Property LCM_Data() As DbSet(Of LCM_Data)
Public Overridable Property LCM_document() As DbSet(Of LCM_document)
End Class
The actual error I'm getting is:
Schema specified is not valid. Errors:
The relationship 'RFI_DataModel.FK_contact_contact_type' was not loaded because the type 'RFI_DataModel.contact' is not available.
The relationship 'RFI_DataModel.FK_contact_implementation' was not loaded because the type 'RFI_DataModel.contact' is not available.
If I remove the 'contact' navigation property from the contact_type and implementation classes, the list view loads without issue.

Unable to cast object of type 'System.Collections.Generic.HashSet

I am trying to use these classes with EF Code-First to add data to the DB. After I populate the object and try to populate the context "dbContext.Tracks.Add(t1)". I get the error "Unable to cast object of type 'System.Collections.Generic.HashSet`1[Vinyl7]' to type 'Vinyl7'" Could anyone point out what I am missing?
Public Class Vinyl7
<Key>
Public Overridable Property mediaId As Long
Public Overridable Property parentMediaId As System.Nullable(Of Long)
Public Overridable Property side As String
<ForeignKey("mediaId"), Required>
Public Overridable Property track As Track
End Class
Public Class Track
<Key>
Public Property mediaId As Long
Public Property title As String
Public Overridable Property Vinyl7T As ICollection(Of Vinyl7) = New HashSet(Of Vinyl7)
End Class
This is my Context
Public Class MediaRepo : Inherits DbContext
Public Sub New()
MyBase.New("name=MediaDB")
End Sub
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
End Sub
Public Property Vinyl7s As DbSet(Of Vinyl7)
Public Property Tracks As DbSet(Of Track)
End Class
This is my Method.
Using db = New MediaRepo
Dim t1 = New Track
t1.title = "Kutchie"
Dim t2 = New Track
t2.title = "Drug Trade"
Dim a = New Media.Vinyl7
a.side = "A"
a.parentMediaId = t1.mediaId
a.track = t1
Dim b = New Media.Vinyl7
b.side = "B"
b.parentMediaId = t1.mediaId
b.track = t2
t1.Vinyl7T.Add(a)
t1.Vinyl7T.Add(b)
db.Tracks.Add(t1)
SaveChanges(db)
End Using
I think your annotation is wrong. You're referencing mediaId as the foreign key to Track, but that's the primary key of your Vinyl7 entity. You probably mean parentMediaId right?
Update your Vinyl7 entity with this, and it will work:
<Key>
Public Overridable Property mediaId As Long
Public Overridable Property side As String
<ForeignKey("mediaId"), Required>
Public Overridable Property track As Track
EDIT
Per your comment, I've updated to a 1..0 to 1 relationship.
Made a quick test app that's working, can you compare it to what you have? Maybe you have something extra thats confusing EF?
Public Class Vinyl7
<Key>
Public Overridable Property mediaId As Long
Public Overridable Property side As String
<ForeignKey("mediaId"), Required>
Public Overridable Property track As Track
End Class
Public Class Track
<Key>
Public Property trackid As Long
Public Property title As String
Public Overridable Property Vinyl7T As Vinyl7
End Class
Module Module1
Public Class MediaRepo : Inherits DbContext
Public Sub New()
End Sub
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
End Sub
Public Property Vinyl7s As DbSet(Of Vinyl7)
Public Property Tracks As DbSet(Of Track)
End Class
Sub Main()
Dim db As New MediaRepo
Dim vin = New Vinyl7
Dim track As New Track
track.Vinyl7T = vin
db.Tracks.Add(track)
db.SaveChanges()
End Sub
End Module

entityframework Inherited classes in single table can not use timestamp?

I want to create a TimeStamp field in Inherited class like this:
[Table("TABLE_A")]
public class A
{
public int ID {get;set;}
public string Name {get;set;}
}
[Table("TABLE_B")]
public class B : A
{
public string Address {get;set;}
[TimeStamp]
public byte[] RowVersion {get;set;}
}
but failed, how can I do here ?
You will see error
Type 'B' defines new concurrency requirements that are not allowed for
subtypes of base EntitySet types.
That means exactly what error says. Entity Framework do not support concurrency checks in derived types. You will see same error if you'll add simple concurrency check instead of timestamp:
[Table("TABLE_B")]
public class B : A
{
[ConcurrencyCheck]
public string Address { get; set; }
}
If you will move concurrency checking to base class, then it will work, but only on base type. If you need checking to be performed on derived type, I think you should use Stored Procedure for updating entity.

Problem with EF Code First foreign key inference

I'm working with EF Code First for the first time, and I'm having trouble getting it to infer the relationships between my types. Given these two types:
<Table("grpGroupType")>
Public Class GroupType
<Key()>
Public Property GroupTypeID As Integer
<Required()>
Public Property IsActive As Boolean
<Required()>
<MaxLength(100)>
Public Property Description As String
Public Overridable Property GroupDefinitions() As ICollection(Of GroupDefinition)
End Class
and
<Table("grpGroupDefinition")>
Public Class GroupDefinition
<Key()>
Public Property GroupDefinitionID As Integer
<Required()>
Public Property GroupTypeID As Integer
<Required()>
Public Property IsActive As Boolean
<Required()>
Public Property ScopeValue As Integer?
<Required()>
<MaxLength(100)>
Public Property Description As String
Public Overridable Property GroupType As GroupType
End Class
I can load and save data using the DbContext class, but when I try to access GroupType.GroupDefinitions or GroupDefinition.GroupType, they both return Nothing. My DbContext class is here:
Public Class PD
Inherits DbContext
Public Property GroupDefinitions As DbSet(Of GroupDefinition)
Public Property GroupTypes As DbSet(Of GroupType)
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As ModelConfiguration.ModelBuilder)
modelBuilder.Entity(Of GroupDefinition)().HasKey(Function(b) b.GroupDefinitionID)
modelBuilder.Entity(Of GroupType)().HasKey(Function(b) b.GroupTypeID)
End Sub
End Class
There doesn't seem to be much documentation on key inference, but I did find this blog post and it appears that my classes follow the rules for automatic inference.
Could anyone point me in the right direction?
Try adding
Public Property GroupTypeID As Integer
To your GroupDefinition class.
Even though it shouldn't be needed, and was not needed in earlier versions, it appears that the CTP5 version of EF needs a little more explicit definition so it can pick up relationships. Personally I hope they fix this before RTM.
I figured it out. I had a SelectOne method that was creating the DbContext locally. I created the context in the calling code and passed it to SelectOne, and all works now. Thanks everyone.