Create a user-defined type (UDT) (C#)
Creates a user-defined type (UDT) to use in a table.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Creates a user-defined type with the specified definition.
Does not return anything.
Parameters
Use the CreateTypeAsync method, which belongs to the Database class.
You can also use CreateType, which is the synchronous version of the method.
Method signature
public Task CreateTypeAsync<T>(CreateTypeOptions options = null);
public Task CreateTypeAsync<T>(
string typeName, CreateTypeOptions options = null
);
public Task CreateTypeAsync(
string typeName,
UserDefinedTypeDefinition definition,
CreateTypeOptions options = null
);
| Name | Type | Summary |
|---|---|---|
|
|
The name of the new user-defined type. The name must be unique within the keyspace. If you use a custom user-defined type class and do not specify this parameter, the client attempts to extract it from first from the |
|
|
The definition for the new user-defined type. The definition describes the name and data type of each field in the user-defined type. You can use any supported data type, except for maps, lists, sets, or other user-defined types. See the examples for usage. |
|
Optional.
Options for this operation.
For more information and examples for general options such as timeout, see Customize API interaction.
For options specific to this method, see Method-specific properties of the |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. Whether the command should silently succeed even if a user-defined type with the given name already exists in the keyspace and no new type was created. This option only checks type names. It does not check type definitions. Default: false |
Examples
The following examples demonstrate how to create a user-defined type.
Create a user-defined type
-
Typed
-
Untyped
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
// Define the user-defined type
[UserDefinedType("member")]
public class Member
{
[ColumnName("name")]
public string? Name { get; set; }
[ColumnName("is_active")]
public bool? IsActive { get; set; }
[ColumnName("date_joined")]
public DateOnly? DateJoined { get; set; }
};
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
// Create a user-defined type
await database.CreateTypeAsync<Member>();
}
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
using DataStax.AstraDB.DataApi.Utils;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
// Create a user-defined type
await database.CreateTypeAsync(
"member",
new UserDefinedTypeDefinition
{
Fields = new Dictionary<string, DataAPIType>
{
["name"] = DataAPIType.Text(),
["is_active"] = DataAPIType.Boolean(),
["date_joined"] = DataAPIType.Date(),
},
}
);
}
}
Create a user-defined type only if the type does not exist
Use this option to silently do nothing if a user-defined type with the specified name already exists.
This option only checks type names. It doesn’t check the definition of any existing types.
-
Typed
-
Untyped
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
// Define the user-defined type
[UserDefinedType("member")]
public class Member
{
[ColumnName("name")]
public string? Name { get; set; }
[ColumnName("is_active")]
public bool? IsActive { get; set; }
[ColumnName("date_joined")]
public DateOnly? DateJoined { get; set; }
};
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
// Create a user-defined type
await database.CreateTypeAsync<Member>(
new CreateTypeOptions() { IfNotExists = true }
);
}
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
using DataStax.AstraDB.DataApi.Utils;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
// Create a user-defined type
await database.CreateTypeAsync(
"member",
new UserDefinedTypeDefinition
{
Fields = new Dictionary<string, DataAPIType>
{
["name"] = DataAPIType.Text(),
["is_active"] = DataAPIType.Boolean(),
["date_joined"] = DataAPIType.Date(),
},
},
new CreateTypeOptions() { IfNotExists = true }
);
}
}
Client reference
For more information, see the client reference.