{"id":6035,"date":"2025-09-22T10:21:01","date_gmt":"2025-09-22T17:21:01","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=6035"},"modified":"2025-09-24T13:31:17","modified_gmt":"2025-09-24T20:31:17","slug":"using-the-new-sqlvector-type-with-ef-core-and-dapper","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/using-the-new-sqlvector-type-with-ef-core-and-dapper\/","title":{"rendered":"Using the new SqlVector type with EF Core and Dapper"},"content":{"rendered":"<p>Azure SQL vector support has been generally available for a few months now, and the ecosystem is quickly evolving to make working with vectors in your applications as seamless and efficient as possible.<\/p>\n<p>With the release of <a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/updated-net-and-jdbc-drivers-with-native-vector-data-support-for-high-performance-ai-workload\/\">Microsoft.Data.SqlClient 6.1<\/a>, developers can now take advantage of <strong>binary transport for vector data<\/strong> via the new <code>SqlVector<\/code> class. This significantly improves performance when moving vectors between your application and the database, laying the groundwork for optimized vector handling in popular .NET libraries like:<\/p>\n<ul>\n<li><strong>EF Core 9<\/strong><\/li>\n<li><strong>EF Core 10<\/strong><\/li>\n<li><strong>Dapper<\/strong><\/li>\n<\/ul>\n<p>Here\u2019s how you can start using <code>SqlVector<\/code> in each of these libraries:<\/p>\n<h2>EF Core 9<\/h2>\n<p>To enable binary vector transfer in EF Core 9, make sure to use the appropriate extension <a href=\"https:\/\/www.nuget.org\/packages\/EFCore.SqlServer.VectorSearch#readme-body-tab\">EFCore.SqlServer.VectorSearch<\/a>. Just add the package and configure your model to use <code>SqlVector:<\/code><\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">builder.Services.AddDbContext&lt;MyContext&gt;(options =&gt;\r\n  options.UseSqlServer(\"&lt;connection string&gt;\", o =&gt; o.UseVectorSearch()));<\/code><\/pre>\n<p>then tell EF Core what property of your POCO object should use the vector type:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n{\r\n    modelBuilder.Entity&lt;MyPOCO&gt;().Property(p =&gt; p.Embedding).HasColumnType(\"vector(1536)\");\r\n}<\/code><\/pre>\n<p>and you&#8217;re good to go! Of course if you prefer using Data Annotations instead of Fluent API, that is supported too. The extension also add support for <code>EF.Functions.VectorDistance<\/code> for using <code>VECTOR_DISTANCE<\/code> in T-SQL to find closest vectors to a given query vector.<\/p>\n<h2>EF Core 10<\/h2>\n<p>EF Core 10 introduces <a href=\"https:\/\/learn.microsoft.com\/en-us\/ef\/core\/providers\/sql-server\/vector-search?tabs=data-annotations\">native support<\/a> for <code>SqlVector<\/code>, making it even easier to work with vector data. You can store and manipulate vectors and use the <code>VECTOR_DISTANCE<\/code> function via the new <code>EF.Functions.VectorDistance()<\/code> method in your LINQ queries. Just tell EF Core what property is mapped to a vector, and it is done:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">public class Blog\r\n{\r\n    \/\/ ...\r\n\r\n    [Column(TypeName = \"vector(1536)\")]\r\n    public SqlVector&lt;float&gt; Embedding { get; set; }\r\n}<\/code><\/pre>\n<p>If you prefer using Fluent API instead of Data Annotation that&#8217;s an option too, of course. Make sure to check out everything that is possible with EF Core and vectors in the official documentation page: <a href=\"https:\/\/learn.microsoft.com\/en-us\/ef\/core\/providers\/sql-server\/vector-search?tabs=data-annotations\">Vector search in the SQL Server EF Core Provider<\/a><\/p>\n<h2>Dapper<\/h2>\n<p>No need to wait for a Dapper update! Since Dapper builds on top of <code>SqlClient<\/code>, you can register a custom Type Handler to map <code>float[]<\/code> or <code>ReadOnlyMemory&lt;float&gt;<\/code> to use <code>SqlVector<\/code> seamlessly:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">public class VectorTypeHandler: SqlMapper.TypeHandler&lt;float[]&gt;\r\n{\r\n    public override float[] Parse(object value)\r\n    {\r\n        return ((SqlVector&lt;float&gt;)value).Memory.ToArray();\r\n    }\r\n\r\n    public override void SetValue(System.Data.IDbDataParameter parameter, float[]? value)\r\n    {\r\n        parameter.Value = value is not null ? new SqlVector&lt;float&gt;(value) : DBNull.Value;\r\n        ((SqlParameter)parameter).SqlDbType = SqlDbTypeExtensions.Vector;\r\n    }\r\n}\r\n<\/code><\/pre>\n<h2>Conclusions<\/h2>\n<p>That\u2019s it! With just a few lines of code, you can unlock high-performance vector operations in your .NET applications. Fast, easy, and future-ready. The end-to-end samples for all the mentioned libraries are available in the following GitHub repo:<\/p>\n<p><a href=\"https:\/\/github.com\/Azure-Samples\/azure-sql-db-vector-search\/tree\/main\/DotNet\">https:\/\/github.com\/Azure-Samples\/azure-sql-db-vector-search\/tree\/main\/DotNet<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Azure SQL vector support has been generally available for a few months now, and the ecosystem is quickly evolving to make working with vectors in your applications as seamless and efficient as possible. With the release of Microsoft.Data.SqlClient 6.1, developers can now take advantage of binary transport for vector data via the new SqlVector class. [&hellip;]<\/p>\n","protected":false},"author":24720,"featured_media":6043,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[444,1],"tags":[696,655,695,569],"class_list":["post-6035","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-azure-sql","tag-dapper","tag-ef-core","tag-entity-framework","tag-vector"],"acf":[],"blog_post_summary":"<p>Azure SQL vector support has been generally available for a few months now, and the ecosystem is quickly evolving to make working with vectors in your applications as seamless and efficient as possible. With the release of Microsoft.Data.SqlClient 6.1, developers can now take advantage of binary transport for vector data via the new SqlVector class. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/6035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/24720"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=6035"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/6035\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/6043"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=6035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=6035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=6035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}